xStream parsing of single level XML

I have some XML that is a single level with an attribute that I can not get to move from XML to Object:

<?xml version="1.0" encoding="utf-8"?>
<response status="426">
You can add 15 clients with your current plan. 
</response>

This xml is represented as this 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;
    }

Using this code I can get XStream to find the 'status' attribute but I can not seem to find the text value of the response node.

    // 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);

In this case the response object has the status populated but not the text.

Seems pretty basic and I can get full objects to move back and forth cleanly when there are tags within the root node but for this single tag case I can't get the content.

I see references to 'mixed xml not being supported', does the xml at the top represent 'mixed'?

Answers


You need to use the ToAttributedValueConverter which supports the definition of one field member that will be written as value and all other field members are written as attributes.

It is easy to do this using xstream annotations as shown below:

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);
    }
}

Need Your Help

Show a toast message when entering a number in Edit text

android if-statement numbers toast

I'd like to show a toast message when entering a number less than 180 , I'm a beginner but I need some help , so i want to show a specific message when entering a number less than 180 , so what to ...

Less compilation on client-side

javascript asp.net css less dotless

I have a .LESS stylesheet, I found a way to change variables value throw JavaScript with an old modified version of less.js

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.