如何转换ArrayList< Object>使用JAXB的XML? [英] How to Convert ArrayList<Object> to a XML using JAXB?

查看:208
本文介绍了如何转换ArrayList< Object>使用JAXB的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JAXB将ArrayList转换为xml ..

I am trying to convert ArrayList to xml using JAXB..

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
调用SEVERE:Servlet.service()对于带有
路径[/ Spring3-LDAP-WebService]的上下文中的servlet [spring]抛出异常[请求处理
失败;嵌套异常是javax.xml.bind.JAXBException:
java.util.ArrayList或它的任何超类都知道这个
上下文。]的根本原因是javax.xml.bind。 JAXBException:类
java.util.ArrayList或它的任何超类都知道这个
上下文。
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)a t
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)............

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)...........

请帮忙!
谢谢。

Please help! Thanks.

推荐答案

尝试创建一个包装列表并使其成为xml根的类,例如:

Try to create a class that wraps your list and make it a xml root, e.g.:

@XmlRootElement
class LDAPUsers {
    private List<LDAPUser> users;
    ... get ... set ... constructor 
}

然后编组LDAPUsers对象。

Then marshal LDAPUsers object.

这篇关于如何转换ArrayList&lt; Object&gt;使用JAXB的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆