java 如何使用 JAXB 编组/解组 MyBean 的集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2315466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:34:42  来源:igfitidea点击:

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

javacollectionsjaxbmarshallingunmarshalling

提问by marklai

I have a MyBean annotated

我有一个 MyBean 注释

@XmlRootElement
public class MyBean ...

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

编组/解组 MyBean 没有问题,例如

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?

如何使用 JAXB 编组/解组集合或列表?

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)

采纳答案by Teja Kantamneni

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

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