tags:

views:

43

answers:

1

Suppose I have a JAXB element like this:

@XmlElement
private double value;

this will generate XML like this:

<value>3.14159</value>

Now, how do I modify my declaration (or customize JAXB marshalling) to generate XML like this instead:

<value type="double">3.14159</value>

The type attribute will always have the same value, i.e. "double".

Thanks!

+2  A: 

You need to a define a class that encapsulates the combination of the double and the string, and then annotate a static fixed value:

public class MyDouble {
    @XmlValue
    private double value;

    @XmlAttribute(name="type")
    private final static String TYPE = "double";

}

So then your code becomes:

@XmlElement
private MyDouble value;
skaffman
Perfect. I always forget about @XmlValue. Thanks!
Dave Ray