9

viaBookに追加するなど、複数のオブジェクトをマーシャリングしようとしています。私はこのセットアップを使い始めます:BookListssetBookslst()JAXBContext

jaxbContext = JAXBContext.newInstance(BookLists.class);

 jaxbMarshaller.marshal(lists, result);

ただし、次の実行時例外が発生します。

javax.xml.bind.JAXBException: com.jaxb.example.marshall.Book もそのスーパークラスもこのコンテキストで認識されていません]

私のタイプは次のように定義されています。

本 :-

@XmlRootElement(name="book")
public class Book {

     private String title;
     private int year;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
}

ブックリスト :-

@XmlRootElement(name="lists")
public class BookLists {
List<Book> bookslst;

public List getBookslst() {
    return bookslst;
}

public void setBookslst(List bookslst) {
    this.bookslst = bookslst;
}

}

マーシャル コード:-

Book book;
    BookLists lists=new BookLists();
    List lst=new ArrayList();
    book = new Book();
    book.setTitle("Book title");
    book.setYear(2010);
    lst.add(book);
    book = new Book();
    book.setTitle("Book title1");
    book.setYear(2011);
    lst.add(book);
    lists.setBookslst(lst);
    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(BookLists.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter result = new StringWriter();

        jaxbMarshaller.marshal(lists, result);
        String xml = result.toString();
        System.out.println(xml);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

注釈を付けようとしています@XMLSeeAlso(Ref:- JAXB Exception: Class not known to this context )。この注釈は、私のバージョンでは利用できません。

4

6 に答える 6

12

デフォルトでは、JAXB (JSR-222) 実装はパブリック アクセサー メソッドを調べます。get/set メソッドにBookパラメーターを追加できます。List

public List<Book> getBookslst() {
    return bookslst;
}

public void setBookslst(List<Book> bookslst) {
    this.bookslst = bookslst;
}

@XmlElementまたは、アノテーションを使用してプロパティのタイプを指定することもできます。

@XmlElement(type=Book.class)
public List getBookslst() {
    return bookslst;
}

また、JAXB 実装がプロパティではなくフィールドをイントロスペクトするように指定することもできます。

@XmlRootElement(name="lists")
@XmlAccessorType(XmlAccessType.FIELD)
public class BookLists {
    List<Book> bookslst;
}

アップデート

Marshallar.Marshall に BookList の代わりに List を追加する別の方法はありますか?

@XmlAnyElement(lax=true)アノテーション を活用する汎用 List ラッパー オブジェクトを作成できます(参照: http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html )。List次に、で注釈が付けられたすべての a をコールド ハンドルし@XmlRootElementます。

リスト

package forum12323397;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Lists<VALUE> {

    private List<VALUE> values = new ArrayList<VALUE>();

    @XmlAnyElement(lax=true)
    public List<VALUE> getValues() {
        return values;
    }

}

デモ

package forum12323397;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Lists.class, Book.class);

        Lists<Book> lists = new Lists<Book>();

        Book book1 = new Book();
        book1.setTitle("A Book");
        book1.setYear(2001);
        lists.getValues().add(book1);

        Book book2 = new Book();
        book2.setTitle("Another Book");
        book2.setYear(2007);
        lists.getValues().add(book2);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(lists, System.out);
    }

}

出力

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lists>
    <book>
        <title>A Book</title>
        <year>2001</year>
    </book>
    <book>
        <title>Another Book</title>
        <year>2007</year>
    </book>
</lists>
于 2012-09-07T18:41:44.573 に答える
0

両方のクラスを JAXBContext.newInstance 呼び出しに追加してみてください。

JAXBContext.newInstance(BookLists.class, Book.class);

クラスを使用すると、これが機能します:

public static void main(String [] args) throws JAXBException {
    BookLists books = new BookLists();

    String [] titles = {"To Kill a Mockingbird", "Twelve Angry Men", "Are You My Mother", "Green Eggs And Ham"};

    List<Book> list = new ArrayList<Book>();
    for (String title : titles) {
        Book book = new Book();
        book.setTitle(title);
        list.add(book);
    }
    books.setBookslst(list);

    JAXBContext jc = JAXBContext.newInstance(BookLists.class, Book.class);
    Marshaller nm = jc.createMarshaller();
    nm.setProperty("jaxb.formatted.output", true);
    nm.marshal(books, System.out);

}

出力:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lists>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book">
    <title>To Kill a Mockingbird</title>
    <year>0</year>
</book>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book">
    <title>Twelve Angry Men</title>
    <year>0</year>
</book>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book">
    <title>Are You My Mother</title>
    <year>0</year>
</book>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book">
    <title>Green Eggs And Ham</title>
    <year>0</year>
</book>
</lists>
于 2012-09-07T18:35:20.097 に答える
0

使用する

List<Book> lst=new ArrayList<Book>();

それ以外の :

List lst=new ArrayList();

また、以下のように BoolList を定義します。

@XmlRootElement(name="lists")
public class BookLists {
List<Book> bookslst;

public List<Book> getBookslst() {
    return bookslst;
}

public void setBookslst(List<Book> bookslst) {
    this.bookslst = bookslst;
}

}
于 2012-09-07T18:38:22.700 に答える
0

実際の出力:

<lists>
    <book>
        <title>A Book</title>
        <year>2001</year>
    </book>
    <book>
        <title>Another Book</title>
        <year>2007</year>
    </book>
</lists>

期待される出力:

<lists>
    <book>
        <title>A Book</title>
        <year>2001</year>
        <title>Another Book</title>
        <year>2007</year>
    </book>
</lists>

積極的に提案を探しています。

于 2021-04-09T09:50:28.387 に答える