我有一些XML是與我不能從XML轉移到對象中的屬性單級:XSTREAM解析
<?xml version="1.0" encoding="utf-8"?>
<response status="426">
You can add 15 clients with your current plan.
</response>
此XML表示這個POJO
public class ClientGenericResponse {
String response;
String status;
ClientID client_id;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public ClientID getClient_id() {
return client_id;
}
public void setClient_id(ClientID clientId) {
client_id = clientId;
}
使用此代碼,我可以得到的XStream,找到「狀態」屬性,但我似乎無法找到響應節點的文本值。
// Map response object
xstream = new XStream();
xstream.alias("response", ClientGenericResponse.class);
xstream.useAttributeFor(ClientGenericResponse.class, "status");
xstream.aliasField("status", ClientGenericResponse.class, "status");
// Send request (this retrieves the xml above)
String xmlResponse = Utility.sendRequest(xml, true);
ClientGenericResponse response = (ClientGenericResponse)xstream.fromXML(xmlResponse);
在這種情況下,響應對象的狀態已填充,但不是文本。
似乎很基本,我可以得到完整的對象乾淨地來回移動,當有根節點內的標籤,但對於這個單標籤的情況下,我無法獲得內容。
我看到「不支持混合XML」來引用,並在頂部的XML代表「混合」?
對於比較起見,你可以使用'@ XmlValue'標註爲'response'財產JAXB映射這樣的情景: //blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html –