Reputation: 4284
Environment: JAXB 2.1.2 with EclipseLink MOXy
Requirement:
I would like to get such an XML when marshalled:
<?xml version="1.0" encoding="UTF-8"?>
<root id="id123">
<email>[email protected]</email>
<address type="short">...</address>
</root>
I model this with these two classes:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="root")
public class ClassA {
@XmlAttribute(name="id")
private String id = null;
@XmlElement(name="address")
private Address addr = new Address();
// and some getters setters
}
and
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
@XmlElement(name="address")
private String address = null;
@XmlAttribute(name="type")
private String type = null;
}
What I get is this, where address gets nested twice:
<?xml version="1.0" encoding="UTF-8"?>
<root id="id123">
<email>[email protected]</email>
<address type="short">
<address>...</address>
</address>
</root>
How can I remove one hierarchy?
Upvotes: 4
Views: 6535
Reputation: 149007
You could do the following leveraging @XmlValue
:
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
@XmlValue
private String address = null;
@XmlAttribute(name="type")
private String type = null;
}
For More Information
Upvotes: 7