I am trying to parse an XML file using JAXB which contains a list of elements. The class of elements depends on the value of the element in XML.
This is an outdated system, and I cannot easily change the input format.
For example, given the following XML and class definitions:
<root>
<type>a</type>
<item>
<a>a1</a>
</item>
<item>
<a>a2</a>
</item>
</root>
@XmlRootElement(name = "root")
public class Root {
@XmlElement
String type;
@XmlElement(name="item")
List<Item> items;
}
public class Item {}
public class ItemA extends Item {
@XmlElement
String a;
}
public class ItemB extends Item {
@XmlElement
String b;
}
As it now works, the list of elements contains two Item objects.
I need a list of items in the resulting Root object to contain two ItemA objects, one with a = "a1" and the other with a = "a2".
If the item is of type "b", I need the list of items to contain ItemB objects.
Only one type element will be specified in a single XML file.
I saw several solutions using attribute values, but none of them used element values.