6.2.7.7
Wrapping Repeated Elements: XmlElementWrapper
With a repeatable XML element you may want to distinguish between
a list that is absent and an empty list. For this, you need some
additional element bracketing, or "wrapping", for the repeated element,
as indicated in the XML snippet below.
<parent>
<wrapper>
<item>A</item>
<item>B</item>
<item>C</item>
</wrapper>
</parent>
You instruct JAXB to generate this additional element by adding
the annotation XmlElementWrapper
to a collection
type attribute.
@XmlType( name="ParentType" )
public class ParentType {
protected List item;
public ParentType(){ ... }
@XmlElement( name="item" )
@XmlElementWrapper( name="wrapper" )
public List getItem(){
if( item == null ) item = new ArrayList();
return item;
}
}