Level: Intermediate Kunal Mittal (kunal@kunalmittal.com), Director, Domestic TV IT, Sony Pictures Entertainment
09 May 2006 Curious about XMLBeans? This advanced, easy-to-use XML-Java binding technology lets you access an XML file just like you would any Java™ object or JavaBean. Find out more about XMLBeans technology, including how to use it with Apache Geronimo and how it simplifies and streamlines service-oriented architecture (SOA) development.
The XMLBeans technology lets you work with an XML structure just as you'd work with a
JavaBean. Using a tool like XMLBeans simplifies SOA
development by reducing the development and testing time required for your services, thus
increasing the agility of your service delivery.
This article uses Apache Geronimo as the deployment container for the XMLBeans that you
write (though you can use any server). The article assumes you've worked with XML in the past and are familiar with concepts like XML schemas, XQuery, XPath, and so on. Don't worry if you aren't an expert, though, because XMLBeans hides the complexity of
these technologies.
What are XMLBeans?
XMLBeans were introduced by BEA as part of its WebLogic Workshop product. In September
2003, BEA donated Page Flow, XMLBeans, and other technologies to the open source community.
The two projects subsequently launched were Apache Beehive and Apache XMLBeans.
You can think of XMLBeans as an object-relational (OR) mapper -- but for XML files rather
than relational databases. XMLBeans lets you access an XML file just as you do any
Java object or JavaBean. In other words, it’s an XML-Java binding tool.
Castor and Java Architecture for XML Binding (JAXB) are other well-known technologies that
perform similar XML-Java binding. Castor is an interesting technology: In addition to
XML-Java data binding, it provides Java-SQL binding like a typical OR mapping tool.
Castor also supports runtime introspection capabilities; it attempts to match elements and
attributes of XML to classes and fields of a Java class. JAXB is a technology from Sun that
provides some basic capabilities.
In the past, when evaluating an OR tool, you most likely didn't consider whether the tool could represent your objects in XML. Today, with SOA in
the forefront, this has become an important criterion when evaluating such tools. The XMLBeans technology, although the newest, is probably the most advanced of the
XML-Java binding technologies. Experience shows that it's the easiest to use and offers the
best set of features and performance. (A detailed comparison of XML-Java binding
technologies is outside the scope of this article.)
Figure 1 illustrates what an XMLBean does. Behind the scenes, the XML
file is always kept in sync with the bean representation. If you have a file that conforms
to some XML schema, you can work with it using getters and setters as you would in a
JavaBean. XML documents are treated as first-class data objects (meaning they can be used without restriction), accessed in a JavaBean-like manner.
Figure 1. XML-Java binding
Here's an example. Suppose you have an XML schema and a corresponding XML file that
represents a book. This file contains an element called Author
that has a one-to-many relationship with book. This is the case because a book can have
multiple authors, as shown in Listing 1.
Listing 1. XML file for a book object
<mybook>
<title>Pro Apache Beehive</title>
<authors>
<author>Kunal Mittal</author>
<author>Srinivas Kanchanvally</author>
</authors>
<isbn>1590595157</isbn>
</mybook>
|
When you work with this file using XMLBeans, the corresponding Java classes maintain this
relationship.
Listing 2 shows some pseudo-Java code to work with an
XMLBean that results from this XML file.
Listing 2. Pseudo-Java code to work with an
XMLBean
MyBook doc = MyBook.Factory.newInstance();
Book book = doc.addNewBook();
book.setTitle("Pro Apache Beehive");
book.setIsbn("1590595157");
String[] authors = new String[2];
authors[0] = new String("Kunal Mittal");
authors[1] = new String("Srinivas Kanchanavally");
book.setAuthors(authors);
|
XMLBeans and SOA development
It's easy to extrapolate how XMLBeans are related to SOA-based development. XML is at the
heart of most SOA technologies, regardless of whether they use Web services. Even if
you assume that SOA is equivalent to Web services (which is essentially true today,
without debating the issue), as a developer who has worked on an SOA project, you have
undoubtedly done some work with XML. You probably have hands-on experience using XML parsers
to manipulate XML content and then manually translating the XML file into an object
structure that you can work with more readily. You may have parsed the XML and looked for
specific data within those files. Or maybe you've translated one XML structure to another.
XMLBeans solves this problem in an SOA world: It does the heavy lifting for you so you can
concentrate on writing Web services or other SOA services.
When you work with SOA or Web services, you don't write an application server or an XML
parser. Instead, you use something built by a vendor or provided as open source that is
widely adopted and tested and that follows its own life cycle. Then why would you write and
manage the code to translate XML to Java objects? Using XMLBeans helps reduce the
development and testing time for your services, thus making you more agile in service
delivery.
Working with XMLBeans in Apache Geronimo
As mentioned earlier, this article uses Apache Geronimo as the deployment container in the
examples. In this section, you'll write a simple XMLBean, and then deploy it in Geronimo.
To begin, you need an XML schema. You can either start with an XML schema or generate one.
Several XML tools, such as XMLSpy and <oXygen/> XML, do this easily. You can also generate an
XML schema online at the XMLBeans schema generation site (see the Resources section at the bottom of this article for a link).
The example XML
file is shown in Listing 3. (This article doesn't provide any sample
code. You should take an XML file that you've worked with in the past and generate a schema
and then an XMLBean from it. This will enable you to see XMLBeans in action as you read this
article.)
Listing 3. XML example for a book
<?xml version="1.0" encoding="UTF-8"?>
<bookDetail xmlns="http://beehive.apress.com/bookstore/vo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://beehive.apress.com/bookstore/vo
file:/C:/ /code/chapter8/bookdetails.xsd">
<book_Id>1</book_Id>
<title>Pro Apache Beehive</title>
<book_type>Book</book_type>
<author>Kunal Mittal, Srinivas Kanchanvally</author>
<publication>Apress</publication>
<publication_Date>2005-08-31</publication_Date>
<catalogNo>123</catalogNo>
<isbn>1590595157</isbn>
<price>49.99</price>
<comments>The 1st book on Apache Beehive</comments>
<pages>300</pages>
<available>false</available>
</bookDetail> |
You can also generate the XMLBeans directly at the XMLBeans Web site -- you don't have to
download anything or run any utilities. To do so, go to the XMLBeans Schema Validator site
(see Resources for the link) and follow the links. Figure 2
shows a screen capture. Alternatively, you can download the XMLBeans distribution; it contains a
command-line utility and an Ant task that can generate the XMLBean for you.
Figure 2. XML schema validation site
After you've generated the XMLBeans, you have a Java Archive (JAR) file that contains
all the code you need. You must still download the XMLBean executable JARs from the
Apache XMLBeans Web site (see Resources for the link) and enable your container
(Apache Geronimo, in this case) to work with the XMLBeans you created.
If you haven't done so already, download and install Apache Geronimo (see Resources for a link to the download). Let's assume you've installed Apache Geronimo in
c:\java\geronimo-1.0 and XMLBeans in c:\java\xmlbeans-2.1.0. The remainder of this article
refers to these directories as GERONIMO_HOME and XMLBEANS_HOME. If you choose to use a
different directory structure, make sure you modify these directory names appropriately.
Although the use of XMLBeans doesn't require an application server, you can leverage
XMLBeans in Java 2 Platform, Enterprise Edition (J2EE) applications deployed in any
application server, including Geronimo. This article explains the high-level concepts and
shows some pseudo-code that illustrates how to do this. However, you should follow through
using actual files to get the maximum benefit.
Set up your J2EE application
Suppose you're building a simple application to exchange information about books with a
library or Amazon.com via Web services. A classic scenario assumes that you're trying to
consume an Amazon Web service. You can try one of the scenarios mentioned in this article or use an
XMLBean you've created using the steps described earlier.
The first step is to copy the .jar files in your XMLBEANS_HOME\lib directory into your
Enterprise Archive (EAR). Based on how and where
you plan to use the XMLBean, you may need to put these files in APP-INF/lib or WEB-INF/lib.
You can even place them in your GERONIMO_HOME\lib directory and make them available to any
J2EE application deployed on this instance of the server.
Next, deploy your J2EE application in Apache Geronimo, as explained in the
developerWorks article "Deploy J2EE
applications on Apache Geronimo" (developerWorks, January 2006). Now
you're ready to put the capabilities of XMLBeans to work.
Set up your XMLBean
Place the .jar file you built for your XMLBean (using either the utilities provided as part
of the XMLBeans distribution or the XMLBeans Web site) in either APP-INF/lib or WEB-INF/lib.
Now all you need to do is write code against the XMLBean. For this example, you'll write a
simple JavaServer Page (JSP) that contains some Java code to work with the
XMLBean.
Write code to work with the
XMLBean
The code in Listing 2 lets you create new XML documents; it uses
basic JavaBean-type getters and setters to create an XML document using the XMLBean.
Listing 4 contains code that lets you import an XML file into an
XMLBean.
Listing 4. Reading an XML file into an
XMLBean
BookDocument book = null; try { book =
BookDocument.Factory.parse(pathtobookxmlfile); } catch (XmlException e) {
e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } |
Try this type of code in a JSP, and deploy the JSP in Geronimo to see the results.
The developerWorks article "Deploy Web services
in Apache Geronimo" (developerWorks, April 2005) shows how to consume the Amazon Web services and work with them in
Apache Geronimo. As an exercise, you can modify this technique to work with XMLBeans.
Conclusion
In this short but hands-on article, you learned the basics of XMLBeans and how to work
with them. You also picked up the primary steps of how to work with XMLBeans in Apache
Geronimo. Doing so is no different from using XMLBeans in IBM® WebSphere®
Application Server, Apache Tomcat, JBoss, or BEA WebLogic. The topics discussed in this
article apply seamlessly to those products.
This article also explained how XMLBeans can save you time when you're working on SOA
projects. It's the leading Java-XML binding technology in terms of features, flexibility,
and -- most important -- performance.
Resources Learn
Get products and technologies
Discuss
About the author  | 
|  | Kunal Mittal is a consultant specializing in Java technology, J2EE, and Web services technologies. He is the coauthor of, and has contributed to, several books on these topics. He works as a director within the Domestic TV IT group for Sony Pictures Entertainment, where he's responsible for the technical architecture and management of applications for that division. In his spare time he writes for IBM developerWorks, consults on SOA, and is a private pilot. For more information, visit Kunal's Web site at www.kunalmittal.com or contact him at kunal@kunalmittal.com. |
Rate this page
|