JavaAutomating XML/Java mapping with Jato

Automating XML/Java mapping with Jato

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java applications that deal with XML, often must resort to tedious parsing
and mapping code. Data binding is gradually emerging as an effective mechanism
for incorporating XML data into Java applications. The plus side to this
approach is that XML data will look like Java objects which makes it easier for
the Java programmer to manipulate and interact with that data. Such mapping are
usually a two-way street in that they can also take a Java object and serialize
it into XML. The negative side would be performance (by introducing some
overhead) and the inherent incompatibility that exists among the different
binding mechanism.

Jato, is an open-source effort focusing on XML/Java conversion

Jato, is an open-source effort focusing on XML/Java conversion (http://www.krumel.com/jato
). Jato encapsulates the mapping between XML and Java objects into an XML file
(using Jato-specific tags). This allows the developer to focus strictly on the
mapping without paying too much attention to implementation. As the author of
Jato observes, conversion implementations often have similar patterns. By
capturing the mapping in a single place and then allowing Jato handle the
parsing, conversion, and generation, you save many lines of code.

Writing a Jato script is similar to writing an XSLT stylesheet. When going
from Java to XML, you can use the JavaToXml class. After instantiating it, you
need to specify the root element to be used for the output XML and specify a
"helper" class via the setHelperclass()
method. The helper class is the interface between the Jato script (containing
the mapping and instructions) and the Java application. When going the other way
(from XML to Java), you can use the XmlToJava class. Again, there is a helper
class you can use as the interface between Jato script and the application.

Within a Jato script, anything outside the Jato namespace, directly goes to
the output. This allows you to easily provides a structure around the
application specific data and change it without modifying any Java code. The
rest of the script consists of Jato-specific tags. For example, the <Jato:attribute>
element adds an attribute to the output XML file. The <Jato:if> element
along with the <Jato:else> element allow for a conditional test to be done
which could alter the output XML file. Inside the Jato script, you have access
to instances of objects from the Java application and can retrieve their
properties or invoke their methods. If the underlying objects are JavaBeans,
then the naming convention can be used to make the mapping easier to follow.

The engine responsible for parsing Jato scripts and performing the
instructions actually uses JDOM to interact with XML. JDOM is an open-source API
for XML manipulation from Java (www.jdom.org).
Jato script elements all have a Java implementation which you can examine by
looking at the source. Furthermore, you can create your own Jato tags by
implementing specific interfaces. This is very important, because you can extend
the functionality of Jato to fit your needs. Furthermore, the behavior of the
application can be modified by merely changing the Jato script file without
touching any of the actual Java code. This is also a promise of XSLT, but Jato
offers a two-way street and the binding between XML elements and Java objects is
more intuitive and natural. In that regards, Jato is closer to Java/XML data
binding tools like Zeus (from Enhydra) than transformations done via XSLT.

Jato is still under development, but you can get a glimpse of what it can do
by looking at the examples and the sample code. And of course, in the spirit of
open-source software, you can always join the team of Jato developers to make it
even better.

About the Author

Piroz Mohseni is president of Bita
Technologies, focusing on business improvement through the effective use of
technology. His areas of interest include enterprise Java, XML, and e-commerce
applications.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories