我已經返回該XML格式的服務:解組通用的清單與JAXB
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>success</status>
<result>
<project>
<id>id1</id>
<owner>owner1</owner>
</project>
<project>
<id>id2</id>
<owner>owner2</owner>
</project>
</result>
或
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>success</status>
<result>
<user>
<id>id1</id>
<name>name1</name>
</user>
<user>
<id>id2</id>
<name>name2</name>
</user>
</result>
我想用這些來解讀檢索XML課程:
結果:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Response<T> {
@XmlElement
protected String status;
@XmlElementWrapper(name = "result")
@XmlElement
protected List<T> 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<Project> responseProject = (Response<Project>)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<User> responseUser = (Response<User>)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<T> {
@XmlElement
protected String status;
@XmlAnyElement(lax=true)
protected List<T> result;
}
然後用這個代碼測試它:
Response<Project> responseProject = unmarshal(unmarshaller, Project.class, "responseProject.xml");
System.out.println(responseProject.getStatus());
for (Project project:responseProject.getResult()) System.out.println(project);
private static <T> Response<T> unmarshal(Unmarshaller unmarshaller, Class<T> clazz, String xmlLocation) throws JAXBException {
StreamSource xml = new StreamSource(xmlLocation);
@SuppressWarnings("unchecked")
Response<T> wrapper = (Response<T>) unmarshaller.unmarshal(xml, Response.class).getValue();
return wrapper;
}
而且我得到這個異常讀取響應列表:
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to org.test.Project
注意:我無法修改原始XML。除Project和User外還有更多類型。
的以下將幫助:http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html –
嗨,布萊斯,我已經找到你的文章,很好順便說一句,但我得到嘗試讀取列表的異常:線程「main」中的異常java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl無法轉換爲org.test.Project 我將更新問題與嘗試的解決方案。 – Fedy2
您需要確保'JAXBContext'知道所有類,並且集合中的每個項目都用'@ XmlRootElement'註解。 –