Currently I have a number of different xml elements wrappers in a class.
I want to add a single attribute to the xml tags for the xml elements. This will work as a flag for my application.
Every xml element will have a different attribute value so I thought to pack them together.
For this reason I wrote a new object that has two fields. A generic value field and a string field to use it as the attribute.
Unfortunatly I cannot find a way to extract the value from the value field without the presens of "garbage" xml tag.
Is there any way to do this. To be more clear I present the specific parts of the code.
@XmlRootElement(name = "client")
class Client {
private List<String> names;
private List<Integer> salaries;
private List<Long> socialSecurityNos;
@XmlElementWrapper(name = "names")
@XmlElement(name = "name")
public List<String> getNames() {
return this.names;
}
@XmlElementWrapper(name = "salaries")
@XmlElement(name = "salary")
public List<String> getSalaries() {
return this.salaries;
}
@XmlElementWrapper(name = "socialsecuritynos")
@XmlElement(name = "socialsecurityno")
public List<String> getSocialSecurityNo() {
return this.socialSecurityNos;
}
...
...
}
This produces the following xml
<foo>
<names>
<name>
George
</name>
<name>
John
</name>
</names>
<salaries>
<salaries>
...
...
</salaries>
</salaries>
<socialSecurityNo>
<socialSecurityNo>
...
...
</socialSecurityNo>
</socialSecurityNo>
</foo>
The new Value,attribute pair class that I wrote.
@XmlRootElement(name = "client")
class GenericElement <T> {
private String attribute;
private T value;
public T getValue() {
return this.value;
}
@XmlAttribute(name = "flag")
public String getAttribute() {
return this.attribute;
}
}
And of course i changed the Lists types
@XmlRootElement(name = "client")
class Client {
private List<GenericElement<String>> names;
private List<GenericElement<Integer>> salaries;
private List<GenericElement<Long>> socialSecurityNos;
...
...
I want to get this result.
<foo>
<names>
<name flag="on">
George
</name>
...
<name flag="off">
John
</name>
</names>
....
.....
</socialSecurityNo>
</socialSecurityNo>
</foo>
Instead I get that, with the "garbage" value tag.
<foo>
<names>
<name flag="on">
<value>George</value>
</name>
...
<name flag="off">
<value>Value</value>
</name>
</names>
....
....
....................</value>
</socialSecurityNo>
</socialSecurityNo>
</foo>