ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

The State of JAXB: Availability, Suitability, Analysis, and Architecture The State of JAXB: Availability, Suitability, Analysis, and Architecture

by Satya Komatineni

When working with XML in OO languages, there is little doubt that objects provide distinct advantages as compared to SAX, DOM, or raw XML. This process of working with XML and objects seamlessly is called binding. When your chosen language is Java, there are many alternatives available for binding Java and XML. In contrast, in .NET this choice is made for you by Microsoft with a tool called XSD.exe.

Of the solutions available in Java, the Java Architecture for XML Binding, better known as JAXB, seems to have finally turned the corner and is now part of the Java Web Services Developer Pack. For those watching the Java-XML binding over the last few years, a nagging question has been, "What is the current state of JAXB and how well does it represent the java-XML binding space?" This being a "state of ..." article, the audience will be senior architects that want to get a bird's-eye view of JAXB as well as a critical analysis of the strengths and weaknesses of the tool. Additional references to get into the introductory and development details of JAXB are provided at the end of the article.

JAXB Packaging

As stated earlier, JAXB is part of the Java Web Services Developer pack 1.x. This integrated toolkit contains all of J2EE's XML technologies, including SAX, DOM, web services, and others. The instructions for downloading and installing the web services pack identify the sub-packages and OS-by-OS installation instructions. For example, the instructions for the Windows install will tell you to first uninstall the previous version and then install the new release.

Related Reading

XML Hacks
100 Industrial-Strength Tips and Tools
By Michael Fitzgerald

The download is about 30MB and will instruct you to download a 1.4 JVM if you don't have one already. You will be better off installing a compliant JDK before installing JAXB. The JAXB install is fairly smooth after this. JAXB install will use the JDK paths to fix its internal batch files so that they will run properly using the JVM that is specified. I did run into some issues running xjc.bat with JDK 1.4.2, prompting the thought, "Perhaps I should have gone with JDK 1.3.1 instead." But luckily I found a workaround that I will mention later in this article.

The JAXB Release Notes in the distribution identify the necessary .jar files for working with JAXB. This list of .jar files is important when you decide to include the generated Java classes or .jar files and build them in an IDE-based environment such as Eclipse or JBuilder. Chapter 10 and Chapter 11 of the Java Web Services Tutorial works as a user's guide for JAXB.

The distribution includes binaries, the JAXB spec, JAXB API documentation, and sample applications along with source code. The sample directories in the JAXB distribution are especially useful and almost necessary. These sample directories have Ant build.xml files. These are the files that you will most likely end up using to compile your XML solutions into a .jar that you will eventually include in your projects.

Working with JAXB

JAXB makes use of XML schema definitions for generating the bindings between XML and Java. This current dependency on XML schemas is somewhat debated, but in situations where the schemas are readily available, JAXB works quite well in generating the Java classes. Let me start off this discussion by giving an example each of an XML file and an XSD file that I will use in discussing the JAXB architecture.

Sample XML File

<?xml version="1.0" encoding="UTF-8"?>
<folder id="123" name="folder1">
    <file id="123" name="file1">
        <contents>some text</contents>
    <file id="123" name="file1">
        <contents>some text</contents>

This XML is a representation of files and folders, say, in a content -management system, where files are individual items inside of a folder. A folder has a database id and a short name as attributes, followed by a collection of files that it contains. Each file has a database id and name as attributes, followed by description and contents for holding descriptive data and the real textual content for that file. To work with this data structure in programs via JAXB, we first need to have an XSD representing the meta-content of this data structure.

Corresponding XSD File

Generating or working with XSD files is not very intuitive. Even when you use tools like xmlspy it can still take some getting used to. I have tried collecting some information in the form of an XML Knowledge Folder to help me with these unfamiliar structures. I end up going to this folder every time I need to create some XSDs. Without these notes, it is hard to remember the XSD syntax, not to mention the additional nuances.

Here is an XSD file that corresponds to the above XML file:

<?xml version="1.0" encoding="UTF-8"?>
* Define a type for the file elment as a 
*      1. collection of two elements description
*         and contents
*      2. an attribute of id that is a string
*      3. an attribute of name that is a string
*      4. Notice that although "file" is a type
*         I refrained from calling it "fileType" as
*         it is vague
*      5. That will result in a Java class called 
*         "fileType" as opposed to the intended
*         class name of "file"
<xs:complexType name="file">
        <xs:element name="description" type="xs:string"/>
        <xs:element name="contents" type="xs:string"/>
    <xs:attribute name="id" type="xs:string"/>
    <xs:attribute name="name" type="xs:string"/>

* Now define a folder to be 
*      1. a collection of elements called files of
*         type file
*      2. an attribute of id that is a string
*      3. an attribute of name that is a string
* As a folder is the outer-most element I didn't
* think it is worthwhile to publish it as a type
<xs:element name="folder">
            <xs:element name="file" type="file" 
	          minOccurs="0" maxOccurs="unbounded"/>
        <xs:attribute name="id" type="xs:string"/>
        <xs:attribute name="name" type="xs:string"/>
* Some additional notes on the XSD
*      1. It is necessary to know basic XSDs when
*         working with JAXB
*      2. Even though a folder is a collection of
*         file types  you can't say so directly in xsd
*      3. you have to walk through the element model
*         and say element/complextype/sequence then
*         the elements
*      4. It is also important to note that attribute
*         is a property  of the complexType and not
*         the element

I have annotated the XSD file using comments to clarify syntactical nuances of the XSD to suit an object palette of the intended readers.

XSD to Java Classes

JAXB allows us to go from the XSD to a set of Java interfaces and a supporting cast of other files that will eventually help us work with objects and not XML. As per the documentation, this is accomplished by using a batch file called xjc.bat, which is available in the subdirectory of JAXB\bin. There is an equivalent shell script for UNIX-like operating systems.

xjc.bat -p com.ai.xmltest sample.xsd -d gen

This command line generates the required interfaces and implementation classes from the sample.xsd file as the package com.ai.xmltest and places the results in a directory called gen.

I spent a whole day trying to get this batch file work under JDK 1.4.2 without success. I continue to receive an error message indicating that a content line is not allowed as the first line of an XML file. Lots of references on the Web suggest that this may be a problem at the JDK level. I planned on switching to JDK 1.3.1 to see if that fixed the problem. But instead I have decided to use the more robust route that is used in our project development. This uses the Ant build files that are provided with the JAXB distribution. This approach has the disadvantage of requiring knowledge of Ant and using its tasks in build files. Hopefully, with some examples, the reader can accomplish the same thing.

Pages: 1, 2, 3, 4

Next Pagearrow