javax.xml.bind.annotation
Annotation Type XmlType


@Retention(value=RUNTIME)
@Target(value=TYPE)
public @interface XmlType

Maps a class or an enum type to a XML Schema type.

Usage

The @XmlType annnotation can be used with the following program elements:

See "Package Specification" in javax.xml.bind.package javadoc for additional common information.

Mapping a Class

A class maps to a XML Schema type. A class is a data container for values represented by properties and fields. A schema type is a data container for values represented by schema components within a schema type's content model (e.g. model groups, attributes etc).

To be mapped, a class must either have a public zero arg constructor or a static zero arg factory method. In the absence of either, XmlJavaTypeAdapter annotation can be used to adapt the class. The static factory method can be specified in factoryMethod() and factoryClass() annotation elements.

A class maps to either a XML Schema complex type or a XML Schema simple type. The XML Schema type is derived based on the mapping of JavaBean properties and fields contained within the class. The schema type to which the class is mapped can either be named or anonymous. A class can be mapped to an anonymous schema type by annotating the class with @XmlType(name="").

Either a global element, local element or a local attribute can be associated with an anoymous type as follows:

Mapping to XML Schema Complex Type

Mapping class to XML Schema simple type

A class can be mapped to a XML Schema simple type using the @XmlValue annotation. For additional details and examples, see @XmlValue annotation type.

The following table shows the mapping of the class to a XML Schema complex type or simple type. The notational symbols used in the table are:

Target propOrder ClassBody ComplexType SimpleType
Class {} [property]+ -> elements complexcontent
xs:all
Class non empty [property]+ -> elements complexcontent
xs:sequence
Class X no property -> element complexcontent
empty sequence
Class X 1 [ @XmlValue property] &&
[property]+ ->attributes
simplecontent
Class X 1 [ @XmlValue property ]&&
no properties -> attribute
simpletype

Mapping an enum type

An enum type maps to a XML schema simple type with enumeration facets. The following annotation elements are ignored since they are not meaningful: propOrder() , factoryMethod() , factoryClass() . *

Usage with other annotations

This annotation can be used with the following annotations: XmlRootElement, XmlAccessorOrder, XmlAccessorType, XmlJavaTypeAdapter, XmlEnum. However, XmlAccessorOrder and XmlAccessorType are ignored when this annotation is used on an enum type.

Example 1: Map a class to a complex type with xs:sequence with a customized ordering of JavaBean properties.

   @XmlType(propOrder={"street", "city" , "state", "zip", "name" })
   public class USAddress {
     String getName() {..};
     void setName(String) {..};
 
     String getStreet() {..};
     void setStreet(String) {..};

     String getCity() {..}; 
     void setCity(String) {..};
 
     String getState() {..};
     void setState(String) {..};

     java.math.BigDecimal getZip() {..};
     void setZip(java.math.BigDecimal) {..};
   }

   <!-- XML Schema mapping for USAddress -->
   <xs:complexType name="USAddress">
     <xs:sequence>
       <xs:element name="street" type="xs:string"/>
       <xs:element name="city" type="xs:string"/>
       <xs:element name="state" type="xs:string"/>
       <xs:element name="zip" type="xs:decimal"/>
       <xs:element name="name" type="xs:string"/>
     </xs:all>
   </xs:complexType> 
 

Example 2: Map a class to a complex type with xs:all

 @XmlType(propOrder={})
 public class USAddress { ...}
 
 <!-- XML Schema mapping for USAddress -->
 <xs:complexType name="USAddress">
   <xs:all>
     <xs:element name="name" type="xs:string"/>
     <xs:element name="street" type="xs:string"/>
     <xs:element name="city" type="xs:string"/>
     <xs:element name="state" type="xs:string"/>
     <xs:element name="zip" type="xs:decimal"/>
   </xs:sequence>
 </xs:complexType>

Example 3: Map a class to a global element with an anonymous type.

   @XmlRootElement
   @XmlType(name="")
   public class USAddress { ...}

   <!-- XML Schema mapping for USAddress -->
   <xs:element name="USAddress">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="name" type="xs:string"/>
         <xs:element name="street" type="xs:string"/>
         <xs:element name="city" type="xs:string"/>
         <xs:element name="state" type="xs:string"/>
         <xs:element name="zip" type="xs:decimal"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
 

Example 4: Map a property to a local element with anonmyous type.

   //Example: Code fragment
   public class Invoice {
       USAddress addr;
           ...
       }

   @XmlType(name="")
   public class USAddress { ... }
   } 

   <!-- XML Schema mapping for USAddress -->
   <xs:complexType name="Invoice">
     <xs:sequence>
       <xs:element name="addr">
         <xs:complexType>
           <xs:element name="name", type="xs:string"/>
           <xs:element name="city", type="xs:string"/>
           <xs:element name="city" type="xs:string"/>
           <xs:element name="state" type="xs:string"/>
           <xs:element name="zip" type="xs:decimal"/>
         </xs:complexType>
       ...
     </xs:sequence>
   </xs:complexType> 
 

Example 5: Map a property to an attribute with anonmyous type.


     //Example: Code fragment
     public class Item {
         public String name;
         @XmlAttribute 
         public USPrice price;
     }
    
     // map class to anonymous simple type. 
     @XmlType(name="")
     public class USPrice { 
         @XmlValue
         public java.math.BigDecimal price;
     }

     <!-- Example: XML Schema fragment -->
     <xs:complexType name="Item">
       <xs:sequence>
         <xs:element name="name" type="xs:string"/>
         <xs:attribute name="price">
           <xs:simpleType>
             <xs:restriction base="xs:decimal"/>
           </xs:simpleType>
         </xs:attribute>
       </xs:sequence>
     </xs:complexType>
 

Since:
JAXB2.0
See Also:
XmlElement, XmlAttribute, XmlValue, XmlSchema

Optional Element Summary
 java.lang.Class factoryClass
          Class containing a zero arg factory method for creating an instance of the annotated class.
 java.lang.String factoryMethod
          Name of a zero arg factory method in factoryClass() for creating an instance of the annotated class.
 java.lang.String name
          Name of the XML Schema type which the class is mapped.
 java.lang.String namespace
          Name of the target namespace of the XML Schema type.
 java.lang.String[] propOrder
          Specifies the order for XML Schema elements when class is mapped to a XML Schema complex type.
 

name

public abstract java.lang.String name
Name of the XML Schema type which the class is mapped.

Default:
"##default"

propOrder

public abstract java.lang.String[] propOrder
Specifies the order for XML Schema elements when class is mapped to a XML Schema complex type.

Refer to the table for how the propOrder affects the mapping of class

The propOrder is a list of names of JavaBean properties in the class. Each name in the list is the name of a Java identifier of the JavaBean property. The order in which JavaBean properties are listed is the order of XML Schema elements to which the JavaBean properties are mapped.

All of the JavaBean properties being mapped to XML Schema elements must be listed. If a JavaBean property is marked with @XmlTransient, then it is ignored.

The default ordering of JavaBean properties is determined by @XmlAccessorOrder.

Default:
""

namespace

public abstract java.lang.String namespace
Name of the target namespace of the XML Schema type. By default, this is the target namespace to which the package containing the class is mapped.

Default:
"##default"

factoryClass

public abstract java.lang.Class factoryClass
Class containing a zero arg factory method for creating an instance of the annotated class. The default is this class.

Default:
javax.xml.bind.annotation.XmlType.DEFAULT.class

factoryMethod

public abstract java.lang.String factoryMethod
Name of a zero arg factory method in factoryClass() for creating an instance of the annotated class. The factory method is used during unmarshalling to create an instance of the annotated class. It is intended to be used for classes that do not contain a zero arg constructor.

Default:
""