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

Java 如何使用JAXB将ArrayList转换为XML?,java,xml,jaxb,Java,Xml,Jaxb,我正在尝试使用JAXB将ArrayList转换为xml ArrayList<LDAPUser> myList = new ArrayList<LDAPUser>(); myList = retrieveUserAttributes.getUserBasicAttributes(lastName, retrieveUserAttributes.getLdapContext()); JAXBContext jaxbContext

我正在尝试使用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();
。。。 但它不起作用,我得到了这个错误:

2012年8月27日10:43:58 org.apache.catalina.core.StandardWrapperValve 在上下文中为Servlet[spring]调用SEVERE:Servlet.service 路径[/Spring3 LDAP WebService]引发异常[请求处理] 失败;嵌套异常为javax.xml.bind.JAXBException:class java.util.ArrayList及其任何超类对此都不了解 context.]具有根本原因javax.xml.bind.JAXBException:class java.util.ArrayList及其任何超类对此都不了解 上下文在 com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfoJAXBContextImpl.java:554 在 com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRootXMLSerializer.java:470 在 com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.writeMarshallerImpl.java:314 在 com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.MarshallerImpl.java:243 在 java:96 在 searchldapusersbylastnametowrestcontroller.java:69 在sun.reflect.NativeMethodAccessorImpl.invoke0Native方法在 sun.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:39

请帮忙!
谢谢。

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

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

然后封送LDAPUsers对象。

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

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

然后封送LDAPUsers对象。

我有getter和setter、、、构造函数,如下所示。。公共LDAPUsers{if ldapUser==null{ldapUser=new ArrayList;}}}..是否正确?是的,但在构造函数中,除非以其他方式初始化列表,否则列表将始终为null。如果不需要。我有getter和setter,,,,,构造函数,如下所示。。公共LDAPUsers{if ldapUser==null{ldapUser=new ArrayList;}}}..是否正确?是的,但在构造函数中,除非以其他方式初始化列表,否则列表将始终为null。如果不需要的话。