Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb create xml attribute

I want to create xml from my object, but instead of xml nodes I'd like to create attribute i.e.

@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?

like image 391
Gandalf StormCrow Avatar asked Jun 01 '26 12:06

Gandalf StormCrow


1 Answers

You can use the @XmlAttribute annotation to map to an 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...
}
like image 91
bdoughan Avatar answered Jun 03 '26 01:06

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!