Boris
Boris

Reputation: 1136

jaxb: How can I bind nested element

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 ?

Upvotes: 5

Views: 5432

Answers (1)

skaffman
skaffman

Reputation: 403591

Use @XmlElementWrapper:

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

Upvotes: 12

Related Questions