如何使用JAXB将ArrayList 转换为XML?

时间:2021-01-18 21:45:32

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在路径[/ Spring3-LDAP-WebService]的上下文中调用SEVERE:servlet [spring]的Servlet.service()引发异常[请求处理失败;嵌套异常是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)在javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)的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.

请帮忙!谢谢。

1 个解决方案

#1


4  

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对象。

#1


4  

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对象。