【问题标题】:xStream parsing of single level XML单级 XML 的 xStream 解析
【发布时间】: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 是否代表“混合”?

【问题讨论】:

标签: java xml xstream


【解决方案1】:

您需要使用ToAttributedValueConverter,它支持定义一个字段成员将被写入值,而所有其他字段成员都被写入属性。

使用 xstream 注释很容易做到这一点,如下所示:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;

@XStreamAlias("response")
@XStreamConverter(value=ToAttributedValueConverter.class, strings={"response"})
public class ClientGenericResponse {

    String response;

    @XStreamAlias("type")
    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;
    }

    public static void main(String[] args) {

        XStream xstream = new XStream(); 
        xstream.processAnnotations(ClientGenericResponse.class);

        // Send request (this retrieves the xml above) 
        String xmlResponse = Utility.sendRequest(xml, true);

        ClientGenericResponse response = (ClientGenericResponse)xstream.fromXML(xmlResponse);
    }
}

【讨论】:

  • 天哪,这比我最终解决的解决方案要容易得多。我所做的是操纵该 xml 本身以将其拉入一个可以轻松解析的更大上下文中。另一方面,这正是我想要的。非常酷,TX!
猜你喜欢
  • 2011-09-19
  • 2016-03-16
  • 2017-07-30
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 2011-10-07
  • 2023-03-24
相关资源
最近更新 更多