Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 840
marklai Avatar asked Feb 23 '10 01:02

marklai


1 Answers

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

like image 177
Teja Kantamneni Avatar answered Nov 15 '22 01:11

Teja Kantamneni