How to use JAXB to marshall/unmarshall a collection of MyBean

荒凉一梦 提交于 2019-12-23 09:13:53

问题


I have a MyBean annotated

@XmlRootElement
public class MyBean ...

Marshalling/Unmarshalling MyBean w/o problems, e.g.

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

How can I use JAXB to marshall/unmarshall a Collection or List?

My attempt results in this error:

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:


You have to create another element of type MyBeanList and use it. Something related on SO Using JAXB to unmarshal/marshal a List<String>



来源:https://stackoverflow.com/questions/2315466/how-to-use-jaxb-to-marshall-unmarshall-a-collection-of-mybean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!