user86834
user86834

Reputation: 5645

Is there a JAXB annotation to do this?

Im using JAXB to serialize my Java model, in JAXB you can choose between an @XmlElement or an @XmlAttribute

my model looks like:

public class Link {
    String rel;
    String href;
}

If I annotate the properties in the Link class with @XmlAttribute the link will look like

<link rel="self" href="http://mycompany.com/resource?param=blah" />

if I annotate the properties with @XmlElement the link will look like:

<link>
    <rel>self</rel>
    <href>http://mycompany.com/resource?param=blah</href>
</link>

I have a requirement to make the XML look like this, which is sort of a mix, but I don't want the href tags around the url so using @XmlElement for href won't work:

<link rel="self">http://mycompany.com/resource?param=blah</link>

Is there a way to do this using JAXB?

Upvotes: 2

Views: 106

Answers (1)

bdoughan
bdoughan

Reputation: 149017

You can use @XmlValue for this use case.

@XmlAccessorType(XmlAccessType.FIELD)
public class Link {

    @XmlAttribute
    String rel;

    @XmlValue
    String href;

}

For More Information

Upvotes: 2

Related Questions