java 如何使用 JAXB 将 ArrayList<Object> 转换为 XML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12139996/
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
How to Convert ArrayList<Object> to a XML using JAXB?
提问by user1534466
I am trying to convert ArrayList to xml using JAXB..
我正在尝试使用 JAXB 将 ArrayList 转换为 xml ..
ArrayList<LDAPUser> myList = new ArrayList<LDAPUser>();
myList = retrieveUserAttributes.getUserBasicAttributes(lastName,
retrieveUserAttributes.getLdapContext());
JAXBContext jaxbContext = JAXBContext.newInstance(LDAPUser.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(myList, sw);
System.out.println(sw.toString());
return sw.toString();
... but its not working, I am getting this error:
...但它不起作用,我收到此错误:
27-Aug-2012 10:43:58 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/Spring3-LDAP-WebService] threw exception [Request processing failed; nested exception is javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.] with root cause javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:470) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96) at ie.revenue.spring.RestController.searchLdapUsersByLastNameTwo(RestController.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)...........
2012 年 8 月 27 日 10:43:58 org.apache.catalina.core.StandardWrapperValve 调用严重:Servlet.service() for servlet [spring] in context with path [/Spring3-LDAP-WebService] 抛出异常 [请求处理失败; 嵌套异常是 javax.xml.bind.JAXBException: class java.util.ArrayList 或其任何超类在此上下文中都是已知的。] 根本原因 javax.xml.bind.JAXBException: class java.util.ArrayList 或任何它的超类在此上下文中是已知的。在 com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554) 在 com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:470)在 com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314) 在 com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)在 javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96) at ie.revenue.spring.RestController.searchLdapUsersByLastNameTwo(RestController.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0) at sun.reflect.NativeMethodAccessorImpl.invoke0) .reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)........
Please help! Thanks.
请帮忙!谢谢。
回答by Piotr Gwiazda
Try to create a class that wraps your list and make it a xml root, e.g.:
尝试创建一个包装您的列表并使其成为 xml 根的类,例如:
@XmlRootElement
class LDAPUsers {
private List<LDAPUser> users;
... get ... set ... constructor
}
Then marshal LDAPUsers object.
然后编组 LDAPUsers 对象。