Chapter 33. Using Complex Types
Abstract
33.1. Basic Complex Type Mapping
Overview
Defining in XML Schema
complexType element. The complexType element wraps the rest of elements used to define the structure of the data. It can appear either as the parent element of a named type definition, or as the child of an element element anonymously defining the structure of the information stored in the element. When the complexType element is used to define a named type, it requires the use of the name attribute. The name attribute specifies a unique identifier for referencing the type.
Table 33.1. Elements for Defining How Elements Appear in a Complex Type
| Element | Description |
|---|---|
all | All of the elements defined as part of the complex type must appear in an instance of the type. However, they can appear in any order. |
choice | Only one of the elements defined as part of the complex type can appear in an instance of the type. |
sequence | All of the elements defined as part of the complex type must appear in an instance of the type, and they must also appear in the order specified in the type definition. |
element element children to the definition.
Example 33.1. XML Schema Complex Type
<complexType name="sequence">
<sequence>
<element name="name" type="xsd:string" />
<element name="street" type="xsd:short" />
<element name="city" type="xsd:string" />
<element name="state" type="xsd:string" />
<element name="zipCode" type="xsd:string" />
</sequence>
</complexType>Mapping to Java
@XmlType annotation. If the mapping is for a named complex type, the annotations name is set to the value of the complexType element's name attribute. If the complex type is defined as part of an element definition, the value of the @XmlType annotation's name property is the value of the element element's name attribute.
@XmlRootElement annotation if it is generated for a complex type defined as part of an element definition.
- All Complex Type
- All complex types are defined using the
allelement. They are annotated as follows:- The
@XmlTypeannotation's propOrder property is empty. - Each element is decorated with the
@XmlElementannotation. - The
@XmlElementannotation's required property is set totrue.
Example 33.2, “Mapping of an All Complex Type” shows the mapping for an all complex type with two elements.Example 33.2. Mapping of an All Complex Type
@XmlType(name = "all", propOrder = { }) public class All { @XmlElement(required = true) protected BigDecimal amount; @XmlElement(required = true) protected String type; public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal value) { this.amount = value; } public String getType() { return type; } public void setType(String value) { this.type = value; } } - Choice Complex Type
- Choice complex types are defined using the
choiceelement. They are annotated as follows:- The
@XmlTypeannotation's propOrder property lists the names of the elements in the order they appear in the XML Schema definition. - None of the member variables are annotated.
Example 33.3, “Mapping of a Choice Complex Type” shows the mapping for a choice complex type with two elements.Example 33.3. Mapping of a Choice Complex Type
@XmlType(name = "choice", propOrder = { "address", "floater" }) public class Choice { protected Sequence address; protected Float floater; public Sequence getAddress() { return address; } public void setAddress(Sequence value) { this.address = value; } public Float getFloater() { return floater; } public void setFloater(Float value) { this.floater = value; } } - Sequence Complex Type
- A sequence complex type is defined using the
sequenceelement. It is annotated as follows:- The
@XmlTypeannotation's propOrder property lists the names of the elements in the order they appear in the XML Schema definition. - Each element is decorated with the
@XmlElementannotation. - The
@XmlElementannotation's required property is set totrue.
Example 33.4, “Mapping of a Sequence Complex Type” shows the mapping for the complex type defined in Example 33.1, “XML Schema Complex Type”.Example 33.4. Mapping of a Sequence Complex Type
@XmlType(name = "sequence", propOrder = { "name", "street", "city", "state", "zipCode" }) public class Sequence { @XmlElement(required = true) protected String name; protected short street; @XmlElement(required = true) protected String city; @XmlElement(required = true) protected String state; @XmlElement(required = true) protected String zipCode; public String getName() { return name; } public void setName(String value) { this.name = value; } public short getStreet() { return street; } public void setStreet(short value) { this.street = value; } public String getCity() { return city; } public void setCity(String value) { this.city = value; } public String getState() { return state; } public void setState(String value) { this.state = value; } public String getZipCode() { return zipCode; } public void setZipCode(String value) { this.zipCode = value; } }

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.