开发者

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

开发者 https://www.devze.com 2022-12-20 19:18 出处:网络
I have a MyBean annotated @XmlRootElement public class MyBean ... Marshalling/Unmarshalling MyBean w/o problems, e.g.

I have a MyBean annotated

@XmlRootElement
public class MyBean ...

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

JAXBContext jaxbCtx = JAXBContext.newInstance(MyB开发者_StackOverflow中文版ean.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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消