Reputation: 5318
I would like to de-serialize the following XML using XStream:
<root>
<node att="value">text</node>
</root>
into a Java object with two fields of type String
. How do I do this?
I've seen these questions, but what I need to do is actually the reverse operation.
Upvotes: 0
Views: 230
Reputation: 10931
As hinted in a couple of the serialization examples you found, ToAttributedValueConverter
is about the most direct way to do this. It allows you to handle the fields on a class as XML attributes, with one picked out as the XML body.
With these two classes to receive the data:
@XStreamAlias("root")
public class Root {
private Node node;
}
@XStreamConverter(value = ToAttributedValueConverter.class, strings = {
"text" })
public class Node {
private String att;
private String text;
}
This deserializes correctly:
XStream xstream = new XStream();
xstream.processAnnotations(Root.class);
Root root = (Root) xstream.fromXML(xml);
Upvotes: 2