Reputation: 73
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'?
Upvotes: 1
Views: 2245
Reputation: 274838
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);
}
}
Upvotes: 3