问题
Alright, I am new to JAXB and having trouble finding the correct documentation to help me through this, I have read a bunch and still dont understand what JaxB is doing.
I have a class, Call it Container. It is extended by MyContainer. MyContainer has an additional boolean value in addition to what it inherits from Container.
Anyways, this is how JAXB Marshalls MyContainer
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyContainer name="container" id="1">
<boolVal>false</boolVal>
<ListOfResources> //Using @XmlElementWrapper(name=..
<child>
<ID>Test Comp 1</ID>
</child>
<child>
<ID>Test Comp 2</ID>
</child>
</ListOfResources>
</MyContainer>
What I don't understand is the tag "child". The only thing named child is the name of the list. The list contains objects (Which have a string property, ID) however the object should not be "child" . The real issue however is when I go to Demarshall the class. The list will populate with the listed "childs". I dont see what I am missing. I don't get a JAXB exception, mind you, but I when I check the unmarshalled object the list is unpopulated
EDIT: Clarification
I am sorry, What I am saying is the JAXB will marshall the elements within the list, albeit each element in the list having the name of the list (child). When it goes to demarshall, however, it does not populate the list in the new object.
EDIT2: Current Setter
public void setChildren(List<Resource<IType>> other) {
Iterator iterator = other.iterator();
while(iterator.hasNext()){
Resource<IType> piece =
new Resource<IType>((Resource<IType>) iterator.next());
this.listOfResources.add(piece);
}
}
回答1:
I'm not sure what your problem is. First you said that the list will be populated with the "childs", then you say that the list is unpopulated. Which is it, and how does it differ from what you expect to see? If something isn't getting populated, the most common cause is that you don't have a well-formed setter for that property.
On the naming, an element is always named the same as the property that maps to it. You can change an element's name with @XmlElement(name="..."), or if you want the element to be named after the object referenced by the property (like the objects that make up a list), use @XmlElementRef.
来源:https://stackoverflow.com/questions/6916790/jaxb-unmarshalls-xml-incorrectly