I have a Java bean ala
@XmlRootElement public class Bean {
@XmlElementWrapper(name = "ints") @XmlElement(name = "int")
int[] values;
// constructors, getters, setters, etc...
}
JAXB is producing XML like
<bean>
<ints>
<int>12</int>
<int>34</int>
<int>56</int>
</ints>
</bean>
I would like the array indices to be included on the <int> tags as array position conveys important value. Preferably as attributes like
<bean>
<ints>
<int id='0'>12</int>
<int id='1'>34</int>
<int id='2'>56</int>
</ints>
</bean>
Is there a way to do this?
The order of XML elements does carry information. A sequence of elements has an explicit order, so adding an array index would be redundant, unless you plan on skipping indexes, or having them out-of-order.
Because of this reasoning, JAXB provides no way of doing what you’re asking automatically. If you still want to do this, you’ll need to wrap your values in a class which incorporates the index values as an
@XmlAttributevalue:It would up to you to make sure the
idfields are populated correctly.