Reputation: 57
After object to XML conversion using JAXB for existing POJOs I am getting the XML as:
<USER>
<FIELDNAME>FirstName</FIELDNAME>
<FIELDVALUE>Michael</FIELDVALUE>
<FIELDID>001</FIELDID>
</USER>
But, as per the requirements it has to be like:
<USER>
<FIRSTNAME ID="001">Michael</FIRSTNAME>
</USER>
Hence I modified POJOs as:
User.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="USER")
public class User
{
@XmlVariableNode("fieldName")
List<Field> field;
// getter/setter
}
Field.java
@XmlAccessorType(XmlAccessType.FIELD)
public class Field
{
@XmlTransient
public String fieldName;
@XmlValue
public String fieldValue;
// getter/setter
}
And getting XML somewhat expected(as below) but not exactly:
<USER>
<FIRSTNAME>Michael</FIRSTNAME>
</USER>
The above mentioned scenario is too simple I know but apart from FIRSTNAME there can be many elements depends upon the situation. Now, how and what should I do to get the ID attribute for FIRSTNAME. where I need to declare the ID property or something other that can help me to get what I want.
Upvotes: 0
Views: 33
Reputation: 696
You can use @XmlAttribute
for that.
Do you intend on auto-generating the ID?
Example:
@XmlAttribute(name="ID")
private String id;
Upvotes: 0