The Java EE 5 Tutorial

About the Schema-to-Java Bindings

When you run the JAXB binding compiler against the po.xsd XML schema used in the basic examples (Unmarshal Read, Modify Marshal, Unmarshal Validate), the JAXB binding compiler generates a Java package named primer.po containing 11 classes, making a total of 12 classes in each of the basic examples, as described in Table 17–12.

Table 17–12 Schema-Derived JAXB Classes in the Basic Examples

Class 

Description 

primer/po/Comment.java

Public interface extending javax.xml.bind.Element; binds to the global schema element named comment. Note that JAXB generates element interfaces for all global element declarations.

primer/po/Items.java

Public interface that binds to the schema complexType named Items.

primer/po/ObjectFactory.java

Public class extending com.sun.xml.bind.DefaultJAXBContextImpl; used to create instances of specified interfaces. For example, the ObjectFactory createComment() method instantiates a Comment object.

primer/po/PurchaseOrder.java

Public interface extending javax.xml.bind.Element, and PurchaseOrderType; binds to the global schema element named PurchaseOrder.

primer/po/PurchaseOrderType.java

Public interface that binds to the schema complexType named PurchaseOrderType.

primer/po/USAddress.java

Public interface that binds to the schema complexType named USAddress.

primer/po/impl/CommentImpl.java

Implementation of Comment.java

primer/po/impl/ItemsImpl.java

Implementation of Items.java

primer/po/impl/PurchaseOrderImpl.java

Implementation of PurchaseOrder.java

primer/po/impl/PurchaseOrderTypeImpl.java

Implementation of PurchaseOrderType.java

primer/po/impl/USAddressImpl.java

Implementation of USAddress.java


Note –

You should never directly use the generated implementation classes (*Impl.java in the packagename/impl/ directory). These classes cannot be referenced directly because the class names in this directory are not standardized by the JAXB specification. The ObjectFactory method is the only portable means to create an instance of a schema-derived interface. There is also an ObjectFactory.newInstance(Class JAXBinterface) method that enables you to create instances of interfaces.


These classes and their specific bindings to the source XML schema for the basic examples are described in Table 17–13. .

Table 17–13 Schema-to-Java Bindings for the Basic Examples

XML Schema 

JAXB Binding 

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>

PurchaseOrder.java

<xsd:element name="comment" type="xsd:string"/>

Comment.java

<xsd:complexType name="PurchaseOrderType">
 <xsd:sequence>
 <xsd:element name="shipTo" type="USAddress"/>
 <xsd:element name="billTo" type="USAddress"/>
 <xsd:element ref="comment" minOccurs="0"/>
 <xsd:element name="items" type="Items"/>
 </xsd:sequence>
 <xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>

PurchaseOrderType.java

<xsd:complexType name="USAddress">
 <xsd:sequence>
 <xsd:element name="name" type="xsd:string"/>
 <xsd:element name="street" type="xsd:string"/>
 <xsd:element name="city" type="xsd:string"/>
 <xsd:element name="state" type="xsd:string"/>
 <xsd:element name="zip" type="xsd:decimal"/>
 </xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>

USAddress.java

<xsd:complexType name="Items">
 <xsd:sequence>
 <xsd:element name="item" minOccurs="1" maxOccurs="unbounded">

Items.java

<xsd:complexType>
 <xsd:sequence>
 <xsd:element name="productName" type="xsd:string"/>
 <xsd:element name="quantity">
 <xsd:simpleType>
 <xsd:restriction base="xsd:positiveInteger">
 <xsd:maxExclusive value="100"/>
 </xsd:restriction>
 </xsd:simpleType>
 </xsd:element>
 <xsd:element name="USPrice" type="xsd:decimal"/>
 <xsd:element ref="comment" minOccurs="0"/>
 <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
 </xsd:sequence>
 <xsd:attribute name="partNum" type="SKU" use="required"/>
</xsd:complexType>

Items.ItemType

</xsd:element>
 </xsd:sequence>
</xsd:complexType>
 
<!-- Stock Keeping Unit, a code for identifying products -->
 
<xsd:simpleType name="SKU">
 <xsd:restriction base="xsd:string">
 <xsd:pattern value="\d{3}-[A-Z]{2}"/>
 </xsd:restriction>
</xsd:simpleType>
 
</xsd:schema>