如何使用JAXB编组/解组Bean先生的集合

时间:2010-02-23 01:02:18

标签: java collections jaxb marshalling unmarshalling

我有MyBean注释

@XmlRootElement
public class MyBean ...

编组/解组MyBean没有问题,例如

JAXBContext jaxbCtx = JAXBContext.newInstance(MyBean.class);
Marshaller m = jaxbCtx.createMarshaller();
m.marshal(myBean, writer);

如何使用JAXB编组/取消编组或列表?

我的尝试导致此错误:

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.util.ArrayList" as an element because it is missing an @XmlRootElement annotation]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:304)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:230)

1 个答案:

答案 0 :(得分:3)

您必须创建另一个MyBeanList类型的元素并使用它。与SO Using JAXB to unmarshal/marshal a List<String>

相关的内容