使用JAXB解组通用列表

我有一个返回此XML的服务:

  success   id1 owner1   id2 owner2   

要么

   success   id1 name1   id2 name2   

我想使用这些类解组检索到的XML:

结果

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Response { @XmlElement protected String status; @XmlElementWrapper(name = "result") @XmlElement protected List result; } 

项目

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Project { @XmlElement public String id; @XmlElement public String owner; } 

用户

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class User { @XmlElement public String id; @XmlElement public String name; } 

首先不工作的解决方案

 JAXBContext context = JAXBContext.newInstance(Response.class, Project.class, User.class); Unmarshaller unmarshaller = context.createUnmarshaller(); StreamSource source = new StreamSource(new File("responseProject.xml")); Response responseProject = (Response)unmarshaller.unmarshal(source); System.out.println(responseProject.getStatus()); for (Project project:responseProject.getResult()) System.out.println(project); source = new StreamSource(new File("responseUser.xml")); Response responseUser = (Response)unmarshaller.unmarshal(source); System.out.println(responseUser.getStatus()); for (User user:responseUser.getResult()) System.out.println(user); 

我得到一个空列表。

第二个没有工作的解

灵感来自本文http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html我修改了Response类:

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Response { @XmlElement protected String status; @XmlAnyElement(lax=true) protected List result; } 

然后使用以下代码对其进行测试:

  Response responseProject = unmarshal(unmarshaller, Project.class, "responseProject.xml"); System.out.println(responseProject.getStatus()); for (Project project:responseProject.getResult()) System.out.println(project); private static  Response unmarshal(Unmarshaller unmarshaller, Class clazz, String xmlLocation) throws JAXBException { StreamSource xml = new StreamSource(xmlLocation); @SuppressWarnings("unchecked") Response wrapper = (Response) unmarshaller.unmarshal(xml, Response.class).getValue(); return wrapper; } 

我在读取响应列表时遇到此exception:

 Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to org.test.Project 

注意 :我无法修改原始XML。 项目和用户以外的类型更多。

感谢Blaise Doughan和他的文章,我找到了解决方案。

首先,我们需要文章中提供的Wrapper类:

 @XmlRootElement public class Wrapper { private List items; public Wrapper() { items = new ArrayList(); } public Wrapper(List items) { this.items = items; } @XmlAnyElement(lax=true) public List getItems() { return items; } } 

然后我修改了Response类以便使用它:

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Response { @XmlElement protected String status; @XmlElement protected Wrapper result; ... public Response(String status, List result) { this.status = status; this.result = new Wrapper<>(result); } ... public List getResult() { return result.getItems(); } ... } 

最后是解组代码:

 JAXBContext context = JAXBContext.newInstance(Response.class, Project.class, User.class, Wrapper.class); Unmarshaller unmarshaller = context.createUnmarshaller(); StreamSource source = new StreamSource(new File("responseProject.xml")); Response responseProject = (Response)unmarshaller.unmarshal(source); System.out.println(responseProject.getStatus()); for (Project project:responseProject.getResult()) System.out.println(project); source = new StreamSource(new File("responseUser.xml")); Response responseUser = (Response)unmarshaller.unmarshal(source); System.out.println(responseUser.getStatus()); for (User user:responseUser.getResult()) System.out.println(user); 

我已经将Wrapper类添加到上下文类列表中。

或者,您可以将此批注添加到Response类:

 @XmlSeeAlso({Project.class, User.class}) 

Response类上使用@XmlSeeAlso({Project.class,User.class})的缺点是在列表中的每个实体上生成一些垃圾信息: xmlns:xsi =“http://www.w3.org/2001/XMLSchema -instance“xsi:type =”userAccount“

    self http://localhost:8080/salonea-1.0/rest/user-accounts?offset=0&limit=2   prev    next http://localhost:8080/salonea-1.0/rest/user-accounts?offset=2&limit=2     user 638f502a0e409348ccc2e36c24907f0 michzio@hotmail.com michzio sAmPL3#e 2015-09-03T17:30:03+02:00 1   user 334bc79d142a291894bd71881e38a719 alicja@krainaczarow.com alicja zAczka!00 2015-09-03T17:30:03+02:00 2