Java collections types (java.util.Collection and its subtypes, array, List, and parameterized collection types such as List<Integer>) can be mapped to XML schema in different ways and can be serialized in different ways. The following examples show .NET bindings.
Guideline: By default, a collection type such as List<Integer> maps to an XML schema construct that is a repeating unbounded occurrence of an optional and nillable element. .NET binds the XML schema construct to System.Nullable<int>[]. The element is optional and nillable. However, when marshalling JAXB marshaller will always marshal a null value using xsi:nil.
Example: Collection to a list of nillable elements
//-- Java code fragment @XmlRootElement(name="po") public PurchaseOrder { public List<Integer> items; } //-- Schema fragment <xs:element name="po" type="purchaseOrder"> <xs:complexType name="purchaseOrder"> <xs:sequence> <xs:element name="items" type="xs:int" nillable="true" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> ... //--- JAXB XML serialization <po> <items> 1 </items> <items> 2 </items> <items> 3 </items> </po> <po> <items> 1 </items> <items xsi:nil=true/> <items> 3 </items> </po> //-- .NET auto generated code from schema partial class purchaseOrder { private System.Nullable<int>[] itemsField; public System.Nullable<int>[] items { get { return this.itemsField; } set { this.itemsField = value; } } }
Guideline: This is the same as above except that a collection type such as List<Integer> maps to a repeating unbounded occurrence of an optional (minOccurs="0") but not nillable element. This in turn binds to .NET type int[]. This is more developer friendly. However, when marshalling, JAXB will marshal a null value within the List<Integer> as a value that is absent from the XML instance.
Example: Collection to a list of optional elements
//-- Java code fragment @XmlRootElement(name="po") public PurchaseOrder { @XmlElement(nillable=false) public List<Integer> items; } //-- Schema fragment <xs:element name="po" type="purchaseOrder"> <xs:complexType name="purchaseOrder"> <xs:sequence> <xs:element name="items" type="xs:int" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> ... // .NET auto generated code from schema partial class purchaseOrder { private int[] itemsField; public int[] items { get { return this.itemsField; } set { this.itemsField = value; } } }
Guideline: A collection such as List<Integer> can be mapped to a list of XML values (that is, an XML schema list simple type) using annotation @XmlList. .NET maps a list simple type to a .NET System.string.
Example: Collection to a list of values using @XmlList
//-- Java code fragment @XmlRootElement(name="po") public PurchaseOrder { @XmlList public List<Integer> items; } //-- Schema fragment <xs:element name="po" type="purchaseOrder"> <xs:complexType name="purchaseOrder"> <xs:element name="items" minOccurs="0"> <xs:simpleType> <xs:list itemType="xs:int"/> </xs:simpleType> </xs:element> </xs:complexType> ... //-- XML serialization <po> <items> 1 2 3 </items> </po> // .NET auto generated code from schema partial class purchaseOrder { private string itemsField; public string items { get { return this.itemsField; } set { this.itemsField = value; } } }