As Published In

Oracle Magazine
March/April 2004
TECHNOLOGY: Industry Standard

Bring the Right JAX to Play
By Kelli Wiseth

API binds XML data to Java objects.

XML has become a key technology throughout the enterprise in Web services, business-process management (BPM), enterprise-application integration (EAI), and numerous other initiatives. As a result, Java developers can expect XML to play an increasingly important role in development projects. Fortunately, the APIs in JAX (Java APIs for XML) continue to expand in scope to support working with XML data in myriad ways.

For example, one of the more mature Java APIs for XML, JAXP—the Java API for XML Processing—offers developers flexibility in dealing with XML data, by allowing them to plug in the Document Object Model (DOM) or Simple API for XML (SAX) parser of their choice. According to Mark Scardina, an Oracle group product manager and XML evangelist, "one of the advantages of JAXP from a developer's perspective is that it was designed to be 'parser-agnostic,' so that processing XML is not dependent on any particular vendor's parser." With JAXP, Java programmers can choose their parsing-engine approach (DOM or SAX) and know that the XML documents will be parsed and processed appropriately (see "Parsing XML Efficiently" for more information about DOM, SAX, and Streaming API for XML [StAX] parsing techniques for Java applications).

However, writing Java to use JAXP requires application developers to have intimate knowledge of XML. Developers using JAXP must write code to handle all parsing and processing; for example, creating instances of the DOM parser as needed, using the parser to interpret the XML document, building the DOM tree, traversing the DOM tree to access nodes and data, and then handling the data in the application code. Using JAXP with the SAX parser involves writing code to register content handlers, writing the callback code to handle the events as the parser works through the XML document, and so on. In short, the programming logic required to handle XML data can be nonintuitive to Java programmers.

That's where a relative newcomer to the JAX specifications, JAXB—Java Architecture for XML Binding—comes into play. A completely different approach than that of JAXP, "JAXB deals with XML documents through what can be thought of as 'proxy objects'—it lets developers create or access XML data without having to work directly with the XML structures," says Scardina. Instead of writing code to deal with a DOM tree or writing callback methods to process SAX events, developers write code to deal with Java objects.

When used in the appropriate type of application, JAXB can result in greater productivity and ease of use for Java developers. "It's an architecture that allows developers to obtain the benefits of XML, without having to know the nuts and bolts of it," says Scardina. Another way to think about JAXB is that it plays a role comparable to that of object-relational frameworks that map Java objects to database constructs. For example, "Just as Oracle's TopLink product 'container manages' the database, exposing it as a set of objects, JAXB exposes an XML document as a set of objects," says Scardina.

In short, JAXB provides developers with an object-oriented, Java-based approach to creating bidirectional mapping between XML documents and Java objects.

B Is for Binding

From a high level, JAXB is a framework that comprises both development tools and a runtime engine. Implementers, including Oracle, use the JAXB interfaces and class libraries included with the Java Web Services Developer Pack (JWSDP) to provide their own JAXB-compliant tools to support a few core development and runtime activities:

Binding is the process of generating Java interfaces and classes from an XML schema and using those objects at runtime to logically "bind" XML-document data to corresponding Java class objects. (A JAXB-compliant utility called a binding-compiler is a core component of any JAXB implementation; the binding-compiler is the tool used to generate the interfaces and the classes.)

Unmarshaling is the process of instantiating (creating in memory) objects that represent the XML document's content; unmarshaling the data from an XML document results in that data being encapsulated as a Java object.

Marshaling is the reverse of unmarshaling; that is, the data in a Java object becomes encapsulated as XML content for purposes of writing out that data.

Working with JAXB starts with obtaining the XML schema file (or files) associated with the XML document types that the Java application must support. XML Schema is the XML language used to define XML documents with their datatypes and is key to the ability of XML technology to provide meaning and context to XML data. An XML schema file is basically an XML file, written in the XML Schema language, that defines allowable simple and complex data types of a specific XML document type and the relationships among those types (the elements are hierarchical, in other words). Generally speaking, any application that sends or receives XML documents needs to have an XML schema associated with the XML document to successfully convey the meaning (programmatically) in the XML document's content.

Once the developer has the appropriate XML schema file in hand, however, that person need only use it in conjunction with the compiler-generator to create the Java source files that will bind the XML document data to Java objects. How to invoke or execute a binding-compiler is specific to the particular implementation; for example, Oracle's binding- compiler is invoked as a Java executable (oracle.xml.jaxb) passing the XML schema filename (filename.xsd) as a parameter and providing a directory pathname for the output files.

The generated classes (Java source code files declared as interfaces) contain the setter and getter methods for each of the elements defined in the target filename.xsd; the generated classes, in turn, implement the interfaces. "These Java classes provide a set of get methods, which allow me to simply get the values bound to their types out of the parts of the XML document I'm interested in," says Scardina.

At runtime the Java application receives the data from the XML document by using the get methods to unmarshal the data from the XML document and build Java objects—instances of the classes that were produced by the binding compiler.

For example, says Scardina, "Say I want to have a Web form submit purchase orders to another system as XML data. I obtain the XML schema file that defines the elements, data types, and structure of the purchase order and use the JAXB binding compiler to bind the XML entities into interfaces and classes that can then be called from a JSP."

Fitting the Tool to the Task

As with any other tool, application developers will want to fit JAXB to the tasks for which it is best suited. According to Mark Scardina, JAXB will be most productive for use in e-business applications, especially supporting industry-specific interactions between participants sending and receiving XML data in business documents such as purchase orders or invoices. "You're not going to choose JAXB as an authoring-oriented interface for an XML editor or something like that," says Scardina. "If you're working with XML documents—true XML documents that include mixed content, semistructured or unstructured data, with paragraph tags and things like that—you're not likely to want to manipulate such documents with JAXB. It's not really the appropriate technology for such applications and that is why we have JAXP."

In addition, as a bidirectional framework, JAXB is also best suited for applications that not only need to read XML data but also must modify the data, send it on for additional processing, or make it persistent in its changed form to the repository or source from which it originated.
Next Steps

DOWNLOAD Oracle XDK 10g

LEARN more about JAXB
"Unmarshaling and Marshaling Data: JAXB Insurance Profile System" tutorial

OTN's XML Technology Center

READ the JAXB specification

In short, if you need to get data out of XML content and encapsulate that data in Java objects for manipulation programmatically in Java, JAXB gives you a way to do that relatively easily. One potential drawback to keep in mind, however, is that for each data type in the XML schema, you'll be creating Java objects.

Another potential drawback of the JAXB specification is that it doesn't prescribe how to handle "node order," and depending on the specific implementation, that may be problematic. Explains Scardina, "XML has the concept of document order that specifies an order to the nodes. This is defined in the XML specification, which means that different implementations have to interoperate with that order. But JAXB exposes an XML document and its data as a set of individual objects, and according to the specification, the only requirement is to marshal and unmarshal sibling data to and from XML as a list. This list, however, may not be in an interoperable order."

One of the benefits of Oracle's JAXB implementation is that it leverages DOM behind the scenes—when Oracle's JAXB runtime unmarshals an XML document into Java objects, it binds the documents into a DOM structure—so that a developer can reconstruct and recapture the order if necessary. "When you make calls to get the data, behind the scenes, the Oracle JAXB runtime is doing all the work for you to get a particular value or string out of the DOM object," says Scardina.

Conclusion

Support for JAXB 1.0 is one of the important new features of the Oracle XML Developers Kit (XDK) 10g. Future releases of the XDK will enhance the capabilities of Java developers working with XML and Oracle technologies, for example, by adding support for JAXB customization to the Oracle JAXB binding compiler. In the meantime, you can use the XDK today to start working with XML in a more Java-friendly way, using JAXB.


Kelli Wiseth (kelli@alameda-tech-lab.com) is technology director at Alameda Tech Lab and Research Center.


Please rate this document:

Excellent Good Average Below Average Poor

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy