How can I instruct JAXB to process this ?
XML
<root>
<parent>
<child id="1" name="foo" />
</parent>
<parent>
<child id="3" name="foo2" />
</parent>
<parent>
<child id="4" name="bar2" />
</parent>
<parent>
<child id="2" name="bar" />
</parent>
</root>
Root.java
@XmlRootElement
public class Root {
@XmlElement(name="parent/child")
List<Child> allChildren;
}
This doesn’t work … allChildren is empty.
You could change your model and do the following:
Root
Parent
UPDATE
There are a couple of different ways to accomplish this:
OPTION #1 – Any JAXB Implementation using XmlAdapter
You could use an XmlAdapter to virtually add in the
Parentclass.ChildAdapter
Root
The
@XmlJavaTypeAdapterannotation is used to reference theXmlAdapter.Child
OPTION #2 – Using EclipseLink JAXB (MOXy)
If you are using EclipseLink JAXB (MOXy) as your JAXB (JSR-222) implementation then you could do the following (Note: I’m the MOXy lead):
Root
Child
MOXy’s
@XmlPathannotation works pretty much the way you are trying to use the@XmlElementannotation in your post.For More Information