I currently have this environment on my project:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
For the following XML document
<bobar>
<first>blablabla</first>
<second>blublublu</second>
</bobar>
When I do
context = JAXBContext.newInstance("the.package.structure");
unmarshaller = context.createUnmarshaller();
Bar object = (Bar) unmarshaller.unmarshal("path-to-xml-document");
The Bar object only has one element in the collection, not 2. The First element is completly lost, when I try to do object.getThings(), its size is 1 and the only object inside the collection is an instance of Second. Can someone help me how can I achieve to get both objects in the collection? And if that’s not possible, how can I achieve something similar to this?
The reason I’m doing this is that (in my project logic) every Bobars things collection has a First in its collection, but not every Bar has a Second in its collection, and Foo is a generic class.
Edit:
When I change the order in my XML document, the output is different.
<bobar>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
In this scenario, I get only an instance of First in the collection, and Second is lost. And changing the scenario more, I get interesting results:
public abstract class Foo {
private List<Thing> things;
public List<Thing> getThings() { return this.things; }
}
public abstract class Bar extends Foo {
@XmlElements({@XmlElement(name = "first", type = First.class), @XmlElement(name = "third, type = Third.class)})
public List<Thing> getThings() { return super.getThings(); }
}
public class Bobar extends Bar {
@XmlElements({@XmlElement(name = "second", type = Second.class)})
public List<Thing> getThings() { return super.getThings(); }
}
If I do
<bobar>
<third>bliblibli</third>
<second>blablabla</second>
<first>blublublu</first>
</bobar>
In theory, I think this shouldn’t be validated against the XML Schema generated by that, as the order here is not correct. But besides that, in such scenario, I get Second and First, the Third is lost.
It is not possible to annotate a property on a super type, and have a sub try incrementally add to that mapping. Below is a way you could support all the use cases that you are after. One thing to be cautious of is that all levels in the object hierarchy would support the same set of elements. You would need to use a external means of validation to restrict the desired values.
If
Thingis a class not an interface, andFirstandSecondextendThingthen you may be interested in using@XmlElementRefinstead of@XmlElements(see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html). It will offer you more flexibility, at the cost of some validation (hard to restrict the set of valid values).Bar
We will annotate the
Barwith@XmlTransientso that the JAXB implementation doesn’t process it.Bobar
@XmlElementRefcorresponds to the concept of substitution groups in XML schema. The values matching the property will be based on@XmlRootElementdeclarations.Thing
As JAXB implementations can not use reflection to find all the subclasses of a type, we can use the
@XmlSeeAlsoannotation to help out. If you don’t use this annotation then you will need to include all the subtypes when bootstrapping theJAXBContext.First
We will need to annotate
Firstwith@XmlRootElement:Second
Secondwill also need to be annotated with@XmlRootElement:Demo
input.xml/Output
OTHER FILES
Below are the other files you need to run this example:
Foo