Jaxb create xml attribute

I want to create xml from my object, but instead of XML nodes I would like to create an ie attribute

@XmlRootElement
class MyObject{
 private String name;
 private String age;
 ...getters/setters...
}

And I want my object to create this xml:

<MyObject name="something">
   <age></age>
</MyObject>

How can i do this?

+3
source share
1 answer

You can use annotation @XmlAttributeto map to the XML attribute.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class MyObject{

 @XmlAttribute // Maps to an XML attribute
 private String name;


 private String age; // Maps to an XML element
 ...getters/setters...
}
+5
source

All Articles