SCDJWS Study Guide: JAXB


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

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


Table 1-1 Core Components in a JAXB Implementation 

Component

Description

XML Schema

An XML schema uses XML syntax to describe the relationships among elements, attributes and entities in an XML document. The purpose of an XML schema is to define a class of XML documents that must adhere to a particular set of structural rules and data constraints. For example, you may want to define separate schemas for chapter-oriented books, for an online purchase order system, or for a personnel database. In the context of JAXB, an XML document containing data that is constrained by an XML schema is referred to as a document instance, and the structure and data within a document instance is referred to as a content tree.

Binding
Customizations

By default, the JAXB binding compiler binds Java classes and packages to a source XML schema based on rules defined in Section 5, "Binding XML Schema to Java Representations," in the JAXB Specification. In most cases, the default binding rules are sufficient to generate a robust set of schema-derived classes from a wide range of schemas. There may be times, however, when the default binding rules are not sufficient for your needs. JAXB supports customizations and overrides to the default binding rules by means of binding customizations made either inline as annotations in a source schema, or as statements in an external binding customization file that is passed to the JAXB binding compiler. Note that custom JAXB binding customizations also allow you to customize your generated JAXB classes beyond the XML-specific constraints in an XML schema to include Java-specific refinements such as class and package name mappings.

Binding
Compiler

The JAXB binding compiler is the core of the JAXB processing model. Its function is to transform, or bind, a source XML schema to a set of JAXB content classes in the Java programming language. Basically, you run the JAXB binding compiler using an XML schema (optionally with custom binding declarations) as input, and the binding compiler generates Java classes that map to constraints in the source XML schema.

Implementation of javax.xml.bind

The JAXB binding framework implementation is a runtime API that provides interfaces for unmarshalling, marshalling, and validating XML content in a Java application. The binding framework comprises interfaces in the javax.xml.bind package.

Schema-Derived
Classes

These are the schema-derived classes generated by the binding JAXB compiler. The specific classes will vary depending on the input schema.

Java
Application

In the context of JAXB, a Java application is a client application that uses the JAXB binding framework to unmarshal XML data, validate and modify Java content objects, and marshal Java content back to XML data. Typically, the JAXB binding framework is wrapped in a larger Java application that may provide UI features, XML transformation functions, data processing, or whatever else is desired.

XML Input
Documents

XML content that is unmarshalled as input to the JAXB binding framework -- that is, an XML instance document, from which a Java representation in the form of a content tree is generated. In practice, the term "document" may not have the conventional meaning, as an XML instance document does not have to be a completely formed, selfstanding document file; it can instead take the form of streams of data passed between applications, or of sets of database fields, or of XML infosets, in which blocks of information contain just enough information to describe where they fit in the schema structure.

In JAXB, the unmarshalling process supports validation of the XML input document against the constraints defined in the source schema. This validation process is optional, however, and there may be cases in which you know by other means that an input document is valid and so you may choose for performance reasons to skip validation during unmarshalling. In any case, validation before (by means of a third-party application) or during unmarshalling is important, because it assures that an XML document generated during marshalling will also be valid with respect to the source schema. Validation is discussed more later in this chapter.

XML Output
Documents

XML content that is marshalled out to an XML document. In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.



JAXB Binding Process

The general steps in the JAXB data binding process are:

  1. Generate classes. An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema.
  2. Compile classes. All of the generated classes, source files, and application code must be compiled.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |