【发布时间】:2011-12-02 07:37:26
【问题描述】:
我有一些 XML 是一个单一级别的属性,我无法从 XML 移动到 Object:
<?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 是否代表“混合”?
【问题讨论】:
-
为了比较起见,您可以在 JAXB 中使用
@XmlValue注释为response属性映射此场景:blog.bdoughan.com/2011/06/…