Marcus Leon
Marcus Leon

Reputation: 56699

Using JAXB and skipping an element

We have XML such as shown below. We have a corresponding Team, Players, and Player Java classes. We use JAXB to unmarshall the XML.

<team>
   <players>
      <player>
         <name>Le Bron</name>
         <age>23</age>
      </player>

      <player>
         <name>Shaq</name>
         <age>33</age>
      </player>
   </players>
</team>

What I'd like is to not have the Players class as it doesn't add any value.

I tried just removing the Players class and adding the annotation below to the Team class but it didn't work. I think JAXB is expecting the <Player> elements to be directly one level below Team.

Any ideas?

@XmlElement(name = "Player")
protected List<Player> players;

Upvotes: 4

Views: 287

Answers (1)

skaffman
skaffman

Reputation: 403581

You need to add the @XmlElementWrapper annotation:

@XmlElementWrapper(name = "Players")
@XmlElement(name = "Player")
protected List<Player> players;

Upvotes: 4

Related Questions