I'm starting with JAXB and I'm trying to read the following xml to map it to a class:
<element id="0">
<type>1</type>
<color>0</color>
<size>1</size>
<location>
<absolute>
<absolute-item>top</absolute-item>
<absolute-item>left</absolute-item>
</absolute>
<relative>
<right>0</right>
<left>0</left>
</relative>
</location>
</element>
My problem comes when I try to map the nested elements like absolute, which can contain any number of <absolute-item>
elements. I'm trying this right now:
public class Element {
@XmlAttribute
private int id;
@XmlElement
private int type;
@XmlElement
private int color;
@XmlElement
private int size;
@XmlElementWrapper(name="absolute")
@XmlElement(name="absolute-item")
private ArrayList<String> absoluteItems;
@Override
public String toString() {
return "Element "+id+" {" +
"type=" + type +
", color=" + color +
", size=" + size +
", Location Relative: "+ absoluteItems
+"}";
}
}
I got the simple elements but not the nested one. Apparently I can't annotate to wrappers together, so I don't know how to fix it.
UPDATE:
I'm trying this as suggested. I'm getting an IllegalAnnotationExceptions
because Element$Location.right is not a compilation property, but I don't know what it means. Should I create one more class for the <relative>
element?
public class Element {
@XmlAttribute
private int id;
@XmlElement
private int type;
@XmlElement
private int color;
@XmlElement
private int size;
@XmlElement(name="location")
private Location location;
public static class Location {
@XmlElementWrapper(name="absolute")
@XmlElement(name="absolute-item")
private ArrayList<String> absoluteItems;
@XmlElementWrapper(name="relative")
@XmlElement(name="right")
private int right;
@XmlElement(name="left")
private int left;
@Override
public String toString() {
return "Location{" +
"Absolute=" + absoluteItems +
", relative {right=" + right +
", left=" + left +
"}}";
}
}