JAXB is a mechanism by which a schema or DTD can be used to create Java classes that accurately represent schema or DTD components into a corresponding Java class. Figure 13.1 represents the way in which JAXB creates Java objects from XML schema/DTDs and vice versa. To perform these conversions, JAXB uses the following components: Binding Compiler The binding compiler converts a schema or DTD into a set of Java classes. Typically, the binding compiler converts a DTD element declaration or a complex type schema component into a Java class. The attribute declarations, or an element declaration with a simple type is converted to a property within the Java class. Analogous to JavaBeans accessor methods, the compiler also generates the get and set methods to access these properties. The binding compiler generates both an interface definition and an interface implementation class for the Java class that it creates from the DTD or XML schema. Making the interface definition and implementation class separate provides application developers with the flexibility to define their own implementation classes if required. The binding compiler follows its own internal algorithm to determine the data types from the data types for elements described in the schema or DTD. In case you want to override the default behavior of the binding compiler, you need to create a binding schema using the binding language. A binding schema provides explicit direction to the binding compiler on how to convert an XML schema component to a Java object. Typically, you will need to write a binding schema for complex element declarations. However, most of the time the default mapping of an XML schema data type to a Java data type will be sufficient, and you can save yourself the bother of writing a binding schema. Table 13.1 displays the mapping that the binding compiler uses to convert an XML schema data type to a Java data type. Table 13.1. Java Mapping for XML Schema Data Types xsd:string | java.lang.String | xsd:integer | java.math.BigInteger | xsd:int | int | xsd.long | long | xsd:short | short | xsd:decimal | java.math.BigDecimal | xsd:float | float | xsd:double | double | xsd:boolean | boolean | xsd:byte | byte | xsd:QName | javax.xml.namespace.QName | xsd:dateTime | java.util.Calendar | xsd:base64Binary | byte[] | xsd:hexBinary | byte[] | xsd:unsignedInt | long | xsd:unsignedShort | int | xsd:unsignedByte | short | xsd:time | java.util.Date | xsd:date | java.util.Date | xsd:anySimpleType | java.lang.String | Binding Runtime Framework The binding runtime framework provides the mechanism by which unmarshaling, marshaling, and validation are done on the interfaces and implementation classes derived from the schema. Unmarshaling is the process by which an XML document is read and a tree of objects is constructed from the contents of the XML document. This tree of objects is also referred to as the content tree. Marshaling is the process by which the content tree is written back as an XML document. Validation is the process by which the validity of the content tree is checked against the constraints defined in the source XML schema. A content tree is considered valid if and only if the XML document generated after marshaling the content tree is valid. Binding Language Binding language is an XML-based specification that describes the binding of a source schema to Java classes. The binding language is used to create the binding schema, which provides the binding compiler with an explicit description of how to bind a schema to a set of Java classes. The binding schema can be provided in two ways: inline with the schema declaration, or in an external file. If you want to use the inline method of specifying the schema binding declarations, you need to use the <appinfo> element specified in the XML schema language specifications. The external file method is used when using the binding compiler with a DTD, because the DTD specification does not support the possibility of annotating a DTD with information for an application. |