![]() |
Linking Java Beans and XML With Ease |
|
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: 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 |