Ryan H
Ryan H

Reputation: 1914

How can I tweek Xstream to handle XML to Java objects that include attributes and values?

For example, how would form an object from XML written like this?

<name length="4">Ryan</name>

I would normally alias a class using an annotation to "name" and then have a length and a field for the name. However, this will not work because the second field has no name.

*Edit confusing wording

Upvotes: 0

Views: 575

Answers (2)

Randy Simon
Randy Simon

Reputation: 3324

It has been a while since I used xstream (2+ years) but I do remember using converters to change the way that objects are serialized. Check out http://x-stream.github.io/converters.html. Also this tutorial, http://x-stream.github.io/converter-tutorial.html, has some examples with attributes down towards the bottom.

Upvotes: 1

bdoughan
bdoughan

Reputation: 149057

Why not use JAXB?

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlRootElement
public class Name {

    @XmlValue
    private String name;

    @XmlAttribute
    private int length;
}

Upvotes: 0

Related Questions