Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb: How can I bind nested element

Tags:

java

xml

jaxb

There is my xml:

<parent>
   <children>
      <child>1</child>
      <child>2</child>
   </children>
</parent>

I want to have the following Parent class:

@XmlRootElement
Parent{
   @XmlElement(name="children/child") 
   List<Child> children;
}

I don't want to have class for element 'children'. How should I map field children ?

like image 769
Boris Avatar asked Jun 16 '10 18:06

Boris


1 Answers

Use @XmlElementWrapper:

@XmlRootElement
public class Parent {
   @XmlElementWrapper(name="children")
   @XmlElement(name="child") 
   List<Child> children;
}
like image 122
skaffman Avatar answered Oct 26 '22 16:10

skaffman