By default, the property will be List, and the base implementation will be ArrayList. Of course, you can use JAXB settings to change the underlying implementation, or use your own class with a property like ArrayList (although for the reasons mentioned in other answers, this is rarely a good idea).
Default JAXB Generation
Given your XML schema:
<schema xmlns="http://www.w3.org/2001/XMLSchema"> <complexType name="BookShelf"> <sequence> <choice minOccurs="0" maxOccurs="unbounded"> <element name="newBook" type="string"/> <element name="oldBook" type="string"/> </choice> </sequence> </complexType> </schema>
Using the following command line:
xjc -d out your-schema.xsd
JAXB will generate the following class:
package generated; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "BookShelf", propOrder = { "newBookOrOldBook" }) public class BookShelf { @XmlElementRefs({ @XmlElementRef(name = "newBook", type = JAXBElement.class), @XmlElementRef(name = "oldBook", type = JAXBElement.class) }) protected List<JAXBElement<String>> newBookOrOldBook; public List<JAXBElement<String>> getNewBookOrOldBook() { if (newBookOrOldBook == null) { newBookOrOldBook = new ArrayList<JAXBElement<String>>(); } return this.newBookOrOldBook; } }
Generation Setup
By default, JAXB will have a be List property type with the underlying implementation being an ArrayList. If you want to control the underlying implementation, you can use an external binding file, for example:
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> <jxb:bindings schemaLocation="f3.xsd"> <jxb:bindings node="//xs:complexType[@name='BookShelf']/xs:sequence/xs:choice"> <jxb:property collectionType="java.util.LinkedList"/> </jxb:bindings> </jxb:bindings> </jxb:bindings>
And the following XJC call:
xjc -d out -b binding.xml your-schema.xsd
Instead, you get the following class:
package generated; import java.util.LinkedList; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "BookShelf", propOrder = { "newBookOrOldBook" }) public class BookShelf { @XmlElementRefs({ @XmlElementRef(name = "oldBook", type = JAXBElement.class), @XmlElementRef(name = "newBook", type = JAXBElement.class) }) protected List<JAXBElement<String>> newBookOrOldBook = new LinkedList<JAXBElement<String>>(); public List<JAXBElement<String>> getNewBookOrOldBook() { if (newBookOrOldBook == null) { newBookOrOldBook = new LinkedList<JAXBElement<String>>(); } return this.newBookOrOldBook; } }
Using your own class:
You can also use your own class with a property of type ArrayList (although for the reasons mentioned in other answers, this is rarely a good idea).
package com.example; import java.util.ArrayList; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "BookShelf", propOrder = { "newBookOrOldBook" }) public class BookShelf { @XmlElementRefs({ @XmlElementRef(name = "oldBook", type = JAXBElement.class), @XmlElementRef(name = "newBook", type = JAXBElement.class) }) protected ArrayList<JAXBElement<String>> newBookOrOldBook ; public ArrayList<JAXBElement<String>> getNewBookOrOldBook() { if (newBookOrOldBook == null) { newBookOrOldBook = new ArrayList<JAXBElement<String>>(); } return this.newBookOrOldBook; } }
Additional Information: