viaBook
に追加するなど、複数のオブジェクトをマーシャリングしようとしています。私はこのセットアップを使い始めます:BookLists
setBookslst()
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 )。この注釈は、私のバージョンでは利用できません。