LibraryToggle FramesPrintFeedback

XML Schema complex types are defined using the 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.

Complex type definitions that contain one or more elements have one of the child elements described in Table 12.1. These elements determine how the specified elements appear in an instance of the type.


If a complex type definition only uses attributes, you do not need one of the elements described in Table 12.1.

After deciding how the elements will appear, you define the elements by adding one or more element element children to the definition.

Example 12.1 shows a complex type definition in XML Schema.

XML Schema complex types are mapped to Java classes. Each element in the complex type definition is mapped to a member variable in the Java class. Getter and setter methods are also generated for each element in the complex type.

All generated Java classes are decorated with the @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.


As described in Java mapping of elements with an in-line type, the generated class is decorated with the @XmlRootElement annotation if it is generated for a complex type defined as part of an element definition.

To provide the runtime with guidelines indicating how the elements of the XML Schema complex type should be handled, the code generators alter the annotations used to decorate the class and its member variables.

All Complex Type

All complex types are defined using the all element. They are annotated as follows:

  • The @XmlType annotation's propOrder property is empty.

  • Each element is decorated with the @XmlElement annotation.

  • The @XmlElement annotation's required property is set to true.

Example 12.2 shows the mapping for an all complex type with two elements.

Choice Complex Type

Choice complex types are defined using the choice element. They are annotated as follows:

Example 12.3 shows the mapping for a choice complex type with two elements.

Sequence Complex Type

A sequence complex type is defined using the sequence element. It is annotated as follows:

Example 12.4 shows the mapping for the complex type defined in Example 12.1.

Comments powered by Disqus
loading table of contents...