7.14. JAXB

JAXB (Java Architecture for XML Binding) is a standard for manipulating XML data. The data is made accessible as programming language objects with unique types that correspond to structural constraints imposed on the XML data by the associated schema.

The standard defines a mapping from XML Schema to Java and a mapping from Java to XML Schema. Both mappings map common constructs naturally, for example XML namespaces correspond to Java packages, XML elements to Java value classes with XML attributes and children corresponding to Java properties, XML integer types to Java integer types. Custom classes or adaptors handle constructs that do not have a natural mapping. Mappings can be customized by XML declarations or Java annotations.

Figure 7.7. Mapping Example XML Schema Input

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="purchase" type="OrderType"/>
  <xsd:element name="comment" type="xsd:string"/>

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

  <xsd:complexType name="Address">
    <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="zip" type="xsd:decimal"/>
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="Some Country"/>
  </xsd:complexType>

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

  ...

  <!-- Example adjusted from JAXB 2.2 Specification. -->

Figure 7.8. Mapping Example Java Output

public class OrderType {
  Address getShipTo () {...}
  void setShipTo (Address) {...}
  Address getBillTo () {...}
  void setBillTo (Address) {...}

  String getComment () {...}
  void setComment (String) {...}

  Items getItems () {...}
  void setItems (Items) {...}

  XMLGregorianCalendar getOrderDate () {...}
  void setOrderDate (XMLGregorianCalendar) {...}
};

public class Address {
  String getName () {...}
  void setName (String) {...}
  String getStreet () {...}
  void setStreet (String) {...}
  String getCity () {...}
  void setCity (String) {...}
  int getZip () {...}
  void setZip (int) {...}

  static final String COUNTRY=”Some Country”;
};

public class Items {
  public class ItemType {...}

  List<Items.ItemType> getItem () {...}
}

// Example adjusted from JAXB 2.2 Specification.

The standard also defines the interface to read and write XML data. Besides reading files, the interface can also access data through SAX or DOM. The interface can unmarshal only a part of a document, and, after that part of the document is modified through the binding, marshal the part back into the document.