prev table of contents next

6.2.7.2 The Annotation XmlList

The attribute XmlList instructs JAXB that a list value is to be represented as a blank separated list of values of some simple type rather than a list of individual child elements. The annotated class Sentence

@XmlType
class Sentence {
  @XmlElement
  List<String> word;
}
produces XML such as
<sentence>
  <word>This</word>
  <word>is</word>
  <word>verbose</word>
</sentence>
In contrast, the addition of XmlList
@XmlType
class Sentence {
  @XmlElement
  @XmlList
  List<String> word;
}
results in
<sentence>
  <word>This is terse</word>
</sentence>
Needless to say, using the XML representation of lists for strings is risky unless you can be sure that no string value contains a blank.

Also, remember the restriction for XML Schema's xsd:list, which is only valid for types that are simple according to XML Schema rules. An class such as Paragraph

@XmlType
class Paragraph {
  @XmlElement
  @XmlList      // Not valid - Sentence isn't a simple type.
  List<Sentence> word;
}
is bound to fail as soon as JAXB inspects the annotations. (The Java compiler has no way of knowing that something is amiss here.)


prev table of contents next