java - 单级 XML 的 xStream 解析

标签 java xml xstream

我有一些 XML,它是一个单一级别的属性,我无法将其从 XML 移动到对象:

<?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 是否表示“混合”?

最佳答案

您需要使用 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);
    }
}

关于java - 单级 XML 的 xStream 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8353128/

相关文章:

java - 更换 Xstream 设施

java - JUnit 4导入一类测试用例(用于功能测试)

java - Android 的 localhost 是什么?

java - OnCreateOptions 被调用,但 OnOptionsItemSelected 未被调用

xstream - Xstream 中的继承问题

java - XStream: UnknownFieldException - 没有这样的字段

java - 根据 FOR 循环内的所有条件设置值

java - 为什么我的 hashmap 只取最后一个条目?

xml - cdata-section-elements - 对于不同 XPath 中的相同元素不能被忽略

xml - 作为另一个节点的同级节点的节点内的节点的 xpath