如何使用JAXB编组多个对象? [英] How do I marshall multiple objects using JAXB?
问题描述
我正在尝试编组多个对象,例如预订
通过 setBookslst()
添加到 BookLists
。我开始使用这个 JAXBContext
设置:
I'm trying to marshall multiple objects e.g. Book
added into BookLists
via setBookslst()
. I begin using this JAXBContext
setup:
jaxbContext = JAXBContext.newInstance(BookLists.class);
和
jaxbMarshaller.marshal(lists, result);
但是我得到了以下运行时异常:
I'm given the following runtime exception however:
javax.xml.bind.JAXBException:com.jaxb.example.marshall.Book或其超类的任何
已为此上下文所知]
javax.xml.bind.JAXBException: com.jaxb.example.marshall.Book nor any of its super class is known to this context]
我的类型定义如下。
预订: -
@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;
}
}
BookList: -
@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
注释(参考: - JAXB异常:类未知这个背景)。这个注释在我的版本中不可用。
I am trying to put @XMLSeeAlso
annotations(Ref:- JAXB Exception: Class not known to this context). This annotation is not available in my version.
推荐答案
默认情况下,JAXB(JSR-222)实现检查公共访问器方法。您可以在get / set方法中的 List
上添加 Book
参数。
By default a JAXB (JSR-222) implementation examines the public accessor methods. You could add the Book
parameter on the List
in your get/set methods.
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实现内省字段而不是属性:
You could also specify that your JAXB implementation introspect the fields instead of the properties:
@XmlRootElement(name="lists")
@XmlAccessorType(XmlAccessType.FIELD)
public class BookLists {
List<Book> bookslst;
}
UPDATE
有没有其他方法可以在
Marshallar.Marshall中添加List而不是BookList?
Is there any alternative way to add List instead of BookList in Marshallar.Marshall?
您可以创建一个利用 @XmlAnyElement(lax = true)
注释的通用List包装器对象(参见: http://blog.bdoughan.com/ 2010/08 /使用-XmlAnyElement将到集结generic.html )。然后它冷处理列表
用 @XmlRootElement
注释的任何内容。
You could create a generic List wrapper object that leveraged the @XmlAnyElement(lax=true)
annotation (see: http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html). Then it cold handle a List
of anything annotated with @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>
这篇关于如何使用JAXB编组多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!