<JOX>

Linking Java Beans and XML With Ease

 
Home
 
News
 
FAQ
 
Download
 
Documentation
 
License
 
Wutka Consulting Home
 
    

What is JOX?

JOX is a set of Java libraries that make it easy to transfer data between XML documents and Java beans. You can think of JOX as a special form of Java Object Serialization, using XML as the serialization format.

JOX attempts to match an XML document to the fields of a bean as best it can and will use a DTD when writing an XML document if one is available. It is able to read attributes as well as regular text and can also handle nested objects. Because JOX looks at the Java Bean at runtime, you can copy XML into different classes.

For example, you might have a Person bean that has firstName and lastName properties. You could read in an XML file containing <first-name> and <last-name> properties. You could also read the XML file into a Customer bean that has First_Name and Last_Name properties.

There are several other approaches to Java serialization in XML. For example, there's KBML - The Koala Bean Markup Language. KBML takes the approach of making XML tags that represent parts of a Java Bean. For example, in KBML, you have <property> and <value> tags. The KBML approach will probably handle many more object structures than JOX will, but you must stick to its XML format. There's nothing wrong with the KBML approach, if you find that it works for your application, by all means use it.

The Quick/JXML/Coins suite of libraries from Bill la Forge and JXML, Inc. give you the same XML document flexibility as JOX. The price you pay is that you must use a special schema language called QJML to describe the mapping between XML and Java. Again, if the Quick code fits your needs, use it. Quick is pretty similar to JOX and is much more intelligent when it comes to data mapping. If it automatically figured out what goes where, there'd probably be no reason for JOX to exist.

JOX differs from these other two libraries in that it lets you use any form of XML document and any Java Bean, and you don't have to create a separate schema to describe the mapping between Java and XML. This approach has some limitations, of course:

  • You must use Java Beans (get/set methods specifically). JOX uses introspection to figure out the property names.
  • XML tag names must match bean property names within reason. JOX compares XML tags to bean properties ignoring capitalization, dashes, underscores, colons and dots. The XML tag <first-name> will map successfully to firstName, first_name and fIrSTn_aME.
  • Because XML data is in the form of a tree structure, JOX can't handle bean structures that have circular references unless you use a DTD when writing the XML.
  • JOX tries to convert XML data to the type of the bean property. There is no facility for custom conversions.
  • Without a DTD, JOX uses bean property names as XML tag names.
  • All that being said, JOX is a snap to use and you don't have to learn any new languages or file formats. The JOX readers and writers pop on to InputStreams, OutputStreams, Readers and Writers so you can use them with any existing Java IO streams. JOX can also write a bean out to an DOM Document object so you can pass it to the Xalan XSLT processor.

    Got any questions or suggestions? Feel free to write me at mark@wutka.com