7

我有一个 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)
4

1 回答 1

3

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

于 2010-02-23T01:51:46.527 回答