prev | table of contents | next |
JAXB is an acronym derived from Java Architecture for XML Binding. It constitutes a convenient framework for processing XML documents, providing significant benefits as compared to previously available methods such as the one following the Document Object Model (DOM). In the DOM approach, the parser creates a tree of objects that represents the content and organization of data in the document. The application can then navigate through the tree in memory to access the data it needs. DOM data, however, is contained in objects of a single type, linked according to the XML document's structure, with individual node objects containing an element, an attribute, a CDATA section, etc. Values are invariably provided as strings.
Unmarshalling an XML document with the appropriate JAXB method also results in a tree of objects, with the significant difference being that the nodes in this tree correspond to XML elements, which contain attributes and the content as instance variables and refer to child elements by object references.
The most convenient way to obtain the Java type information
describing the node elements is by compiling an XML schema, usually
written in the
W3C XML Schema Language,
using the JAXB Binding Compiler xjc
. The resulting
set of classes defines the types required for accessing elements,
attributes and other content in a typesafe way.
Schemas written in the XML Schema Language can describe structural
relationships and data types, with a very high level of distinctiveness.
The scalar datatypes of the XML Schema Language are mapped to Java
data types. Lists of values and certain element groupings are
mapped to Java's java.util.List
.
It should be noted that the XML Schema language is capable of defining
XML structures that cannot be bound by a schema compiler. In many of
these cases it is possible to circumnavigate the problem by adding
binding declarations to direct the schema compiler in some
specific way to achieve a successful binding.
The JAXB runtime library, supported with the code generated by the Binding Compiler, provides methods for unmarshalling a document from various sources as well as for marshalling a content tree to various destinations. JAXB also supports marshalling and unmarshalling for SAX, the Simple API for XML.
JAXB uses Java's annotations for augmenting the generated classes with additional information that bridges the gap between what is decribed by an XML schema and the information available (via Java's reflection mechanisms) from a set of Java class definitions. Adding such annotations to existing Java classes prepares them for being used by JAXB's runtime.
As of the time of this writing (March 2009) JAXB is available as version 2.1.10. Versions from 2.0 on contain bug fixes and minor additions. Version 2, as compared to version 1, was a big step ahead and has brought JAXB to a mature level. (JAXB version 1 should not be used any more.)
prev | table of contents | next |