如何使用 JAXB 编组/取消编组 MyBean 集合
我有一个带注释的 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须创建另一个
MyBeanList
类型的元素并使用它。与 SO Using JAXB to unmarshal/marshal a ListYou have to create another element of type
MyBeanList
and use it. Something related on SO Using JAXB to unmarshal/marshal a List<String>