XML has become an almost inevitable part of most projects. XML does provide a multitude of benefits, but most Java coders also are well aware of what a headache playing with XML in Java can be. Java Document Object Model (JDOM) APIs do make life a little easier, compared to the basic Java DOM or SAX APIs, but devising an easier option for working with XML has been an imperative. Developers need a framework that’s focused not on the structure of the XML document but more on presentation of content.

Java-XML Data Binding is just what the doctor ordered. The approach is loosely described as mapping XML into a Java-specific representation and vice versa. XML binding is suited for applications that worry only about the content they get and not about the way it is delivered. So if your application doesn’t really care if the ‘Username’ is conveyed as an independent node or as an attribute of a ‘user’ node, Java-XML Data Binding can greatly simplify your development and maintenance efforts.

The benefits of Java-XML Data Binding include:

  • Developers don’t need previous knowledge of Java-XML APIs like DOM or SAX. For me, this is definitely a huge advantage. Even with some experience with these APIs, it’s still no easy task to pick up on concepts like the root node’s third child node’s second attribute. When managing a project team, asking Java programmers to work with Java objects is a far easier and safer option than asking team members to retrieve data using DOM or SAX.
  • XML data binding takes advantage of XML’s great medium for storing configuration information. You can pick up the configuration once at startup and use in your application; all it takes is defining a mapping for your configuration file and getting the data into an easy-to-use Java object.
  • You can retrieve (unmarshall) data from a certain XML structure and then insert (marshall) it back to a different XML structure. For instance, you can retrieve some values from XML, do some calculations in basic Java, and marshall the results back to a different XML structure. Apply XSL transformation, if necessary, and you could present an HTML screen depicting the results.

Basic concepts
Java-XML Data Binding revolves around these basic concepts:

Marshalling is the process of generating an XML representation for a Java object in memory. This can be done by providing explicit mapping instructions or using the API’s default marshalling rules.

Unmarshalling describes the conversion of an XML representation into a Java object that you can work within your Java code.

Mapping lets you generate the exact XML structure or Java object that you require, although Java-XML binding APIs do follow some default rules for converting an XML representation into its corresponding Java object.

Some current APIs that support Java-XML Data Binding are:

  • Java Architecture for XML Binding (JAXB): This framework for XML data binding is one of the newer XML APIs from Sun. The API is still in early access; the final draft of JAXB v1.0 should be available by Q4 2002.
  • Castor: This mature API that has been around for a few years seems to be the best option available. Castor does a lot more than just Java-XML Data Binding.
  • Quick: This is a fast and simple binding alternative that requires an understanding of binding schemas defined by the creators.
  • Zeus: Currently in Beta 3.5, Zeus achieves binding based on constraints that you define.

Of these options, JAXB probably would be the easiest to sell to your superiors, since it comes from the Sun stable. However, remember that JAXB still is in early access and so is susceptible to major changes.

Castor can be categorized as a mature API that’s already being used quite widely. And Castor is equally adept at mapping Java to SQL tables or LDAP directories. Overall, this API seems to be the best of the lot, at least for the time being. I’ll cover the particulars of Castor Java-XML Data Binding in my next article.