java - 如何使用 JAXB 将 ArrayList<Object> 转换为 XML?

标签 java xml 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();

... 但它不起作用,我收到此错误:

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

求助! 谢谢。

最佳答案

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

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

然后编码 LDAPUsers 对象。

关于java - 如何使用 JAXB 将 ArrayList<Object> 转换为 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12139996/

相关文章:

xml - 如何在 VB.net 中根据架构验证 XML

java - 如何根据 JAXB 中元素的存在分配 boolean 值?

java - JAXB 编译器和属性顺序

python - 从包含 xml 的变量中获取值

java - JAXB/MOXy - afterUnmarshal() 的调用似乎不一致

java - Java 中矩阵的余因子(用于确定矩阵的逆)

java - Postman oAuth2 错误未经授权的授权类型

java - 如何在android中显示3D模型?

java - 命令模式应用的真实世界示例

python - 不打印 xpath 的空结果