SCDJWS Study Guide: JAXB
Printer-friendly version |
Mail this to a friend
JAXB Overview
JAXB is a Java technology that enables you to generate Java classes from XML schemas by means of a JAXB binding compiler. The JAXB binding compiler takes XML schema as input, and then generates a package of Java classes and interfaces that reflect the rules defined in the source schema. These generated classes and interfaces are in turn compiled and combined with a set of common JAXB utility packages to provide a JAXB binding framework. The JAXB binding framework provides:
- Methods for unmarshalling XML instance documents into Java content trees, and then marshalling Java content trees back into XML instance documents.
- Methods for validating XML content as it is unmarshalled and marshaled.
In another word, the combination of the schema derived classes and the binding framework enable one to perform the following operations on an XML document:
- Unmarshal XML content into a Java object representation.
- Access, update, and validate the Java representation against schema constraints.
- Marshal the Java representation of the XML content into XML content. Similar to the unmarshaling process, the generated Java classes must be validated, and checked for conformance with the existing Java classes before converting them into XML.
Some of the advantages associated with using the JAXB framework include:
- Efficient Mechanism: JAXB provides an efficient mechanism for Java developers to simplify the creation and maintenance of XML-enabled Java applications.
- Increased Productivity: Java developers using JAXB are more productive because they are free from the burden of writing complex parsing code.
Implementing JAXB in your Java applications is typically a two-step process:
- One or more schemas are defined as needed, and then the JAXB binding compiler is run against these schemas to generate JAXB packages, classes, and interfaces.
- Java application developers use the generated JAXB packages and the JAXB utility packages in a binding framework to unmarshal, marshal, and validate XML content.
Core Components in a JAXB Implementation

JAXB Binding Process
The general steps in the JAXB data binding process are:
- Generate classes. An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema.
- Compile classes. All of the generated classes, source files, and application code must be compiled.
- Unmarshal. XML documents written according to the constraints in the source schema are unmarshalled by the JAXB binding framework. Note that JAXB also supports unmarshalling XML data from sources other than files/documents, such as DOM nodes, string buffers, SAX Sources, and so forth.
- Generate content tree. The unmarshalling process generates a content tree of data objects instantiated from the generated JAXB classes; this content tree represents the structure and content of the source XML documents.
- Validate (optional). The unmarshalling process optionally involves validation of the source XML documents before generating the content tree. Note that if you modify the content tree in Step 6, below, you can also use the JAXB Validate operation to validate the changes before marshalling the content back to an XML document.
- Process content. The client application can modify the XML data represented by the Java content tree by means of interfaces generated by the binding compiler.
- Marshal. The processed content tree is marshalled out to one or more XML output documents. The content may be validated before marshalling.
To synopsize, using JAXB involves two discrete sets of activities:
- Design Time Binding Process: Generate and compile JAXB classes from a source schema, and build an application that implements these classes
- Run Time Binding Process: Run the application to unmarshal, process, validate, and marshal XML content through the JAXB binding framework
Note: Unmarshalling is not the only means by which a
content tree may be created. Schema-derived content classes also
support the programmatic construction of content trees by direct
invocation of the appropriate factory methods. Once created, a content
tree may be revalidated, either in whole or in part, at any time.
Distinct Advantages of JAXB
Let's reiterate a number of important advantages of using JAXB:
- JAXB simplifies access to an XML
document from a Java program:.
- JAXB allows you to access and process XML data without having to know XML or XML processing. Unlike SAX-based processing, there's no need to create a SAX parser or write callback methods.
- JAXB allows you to access data in non-sequential order, but unlike DOM-based processing, it doesn't force you to navigate through a tree to access the data.
- By unmarshalling XML data through JAXB, Java content objects that represent the content and organization of the data are directly available to your program.
- JAXB uses memory efficiently: The tree of content objects produced through JAXB tends can be more efficient in terms of memory use than DOM-based trees.
- JAXB is flexible:
- You can unmarshal XML data from a variety of input sources, including a file, an InputStream object, a URL, a DOM node, or a transformed source object.
- You can marshal a content tree to a variety of output targets, including an XML file, an OutputStream object, a DOM node, or a transformed data object
- You can unmarshal SAX events -- for example, you can do a SAX parse of a document and then pass the events to JAXB for unmarshalling.
- JAXB allows you to access XML data without having to unmarshal it. Once a schema is bound you can use the ObjectFactory methods to create the objects and then use set methods in the generated objects to create content.
- You can validate source data against an associated schema as part of the unmarshalling operation, but you can turn validation off if you don't want to incur the additional validation overhead.
- You can validate a content tree, using the Validator class, separately from marshalling. For example, you can do the validating at one point in time, and do the marshalling at another time.
- JAXB's binding behavior can be customized in a variety of ways.
JAXB Best Practices
JAXB is particularly useful if you want to:
- Access configuration values from a properties file stored in XML format.
- Develop a tool that can create or modify a configuration properties file represented in XML format.
- Receive data in the form of an XML document, and then access and/or update the data without having to write SAX event handlers or traverse a DOM parse tree.
- Validate data input by a user; for example, from a form presented in a Web browser, where the form data is mapped to an XML document. In such cases, JAXB provides the capability to validate the accuracy of the data using the validation constraints of a schema that describes the data collected from the form.
- Bind an XML document into a Java representation, update the content via Java interfaces, validate these changes against the constraints within the original schema, and then write the updated Java representation back to an XML document.
- Unmarshal an XML document already known to be valid; validation usually performed by the application while unmarshalling the document can be disabled in this case, thereby greatly improving performance over SAX or DOM methods.