A buddy of mine asked me to post this question:
EDIT: He decided to post the question on his own here: JAXB Unmarshalls XML Incorrectly I tried to delete this question but couldn’t.
I am trying to marshall a java class, called MyContainer. This class extends my another class, called Container. Container has a variable called: protected List<ResourceReference<Component>> child
The only variable MyContainer has is the one it inherits from Container. This List, however, will not marshall with JAXB when I have child listed as an XmlList.
It will marshall as an XmlElement, however this is not how I want the data displayed. When it is marshalled as an XmlElement, it lists the attributes in the List, but not ‘heirarchially’ It just lists the two elements as if they were properties of the class, as opposed to belonging to the child list.
How do I get Jaxb to marshall the List (Which is instantiated as an ArrayList) correctly?
EDIT: Here’s an example:
This is what I am getting:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child id="Test Comp 1"/>
<child id="Test Comp 2"/>
</and>
This is more what I expect:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child> /// This would be the class variable of the extended Class, Container
<ResourceRef id="Entry 1">
<ResourceRef id="Entry 2">
</child>
</and>
Admittly, my current results is probably from the List in Container being marked with the @XmlElement annotation. However, JAXB will not marshall anything else..
Annotate your list with
@XmlElementWrapper. See javadoc.