I have a root Xml document (name = "Entity")
that contains one known Xml element (name = "Header")
and another Xml element of unknown name but it is known to have an inner XmlElement(name="label")
Here are possible Xmls:
<Entity>
<Header>this is a header</Header>
<a>
<label>this is element A</label>
<otherElements/>
</a>
</Entity>
<Entity>
<Header>this is a different header</Header>
<b>
<label>this is some other element of name b</label>
<others/>
</b>
</Entity>
Here are my JAXB annotated classes:
@XmlRootElement(name = "Entity")
@XmlAccessorType(XmlAccessType.NONE)
public class Entity {
@XmlElement(name = "Header")
private Header header;
@XmlElements( {
@XmlElement(name = "a", type=LabelledElement.A.class),
@XmlElement(name = "b", type=LabelledElement.B.class)
} )
private LabelledElement labelledElement;
// constructors, getters, setters...
}
@XmlAccessorType(XmlAccessType.NONE)
public abstract class LabelledElement {
@XmlElement
private String label;
@XmlAnyElement
private List<Element> otherElements;
public static class A extends LabelledElement {}
public static class B extends LabelledElement {}
}
This was working great! But then I noticed that it isn't only <a>
and <b>
It could be <c>
, <asd>
and even <anything>
...
So listing the XmlElement(name = "xyz", type = LabelledElement.xyz.class)
is obviously not the right solution.
All I care about is Entity#getLabelledElement()#getLabel()
no matter what the LabelledElement
name is.
Is this even possible with JAXB?