Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to WebLogic Pro

email article
printer friendly
get the code

XMLBeans Bind XML to the Future
Discover a methodology for developing a Web service client from a WSDL to manage inventory in a database
by David Hritz

December 10, 2004

Beehive—the wizard behind much of Workshop's magic—has reached the incubation phase at the Apache Software Foundation. However, before Beehive was even a buzz in our ears, BEA submitted another project to Apache: XMLBeans. As of June 2004, XMLBeans has been accepted as a full-fledged Apache Software Foundation Project.

ADVERTISEMENT

XMLBeans is an XML-Java binding tool. Yes, it is yet another XML-Java binding tool, but it does come with some new perks. XML has proven to be a powerful and useful format for structured data. Current XML-Java binding tools have their benefits and pitfalls, but each could be considered somewhat tedious to use. XMLBeans has overcome this tedium by mapping the XML instance, and underlying schema, to JavaBean-like objects. XML instance data can now be navigated, retrieved, and manipulated through getter and setter accessor methods, an API commonality that any developer can use with ease.

XMLBeans are created through a compilation of XML Schemas. XML Schema is a language used to provide guidelines for XML documents. A schema comprises the rules and regulations that a particular XML document must follow to be considered valid. XML Schemas can become rather complex, depending on the XML instance that they describe. This complexity is nothing to XMLBeans; no matter how complex, your schema can be compiled into XMLBeans.

We'll discus an inventory management system for a fictional automobile dealership written entirely with XMLBeans that demonstrates not only the use of XMLBeans within Web services, but also their use as data objects throughout the process, from the database to the user interface. We'll highlight the important aspects of this application, but you can download the source code for the entire application. This sample application relies on a single database table, which will store our inventory. Most columns are straightforward, except for the last. The STATUS column holds one of two values, new or used, and will be used later to filter our results (see Table 1).

Create a Schema
XMLBeans originate from the compilation of an XML Schema. The inventory management Web service will return a list of vehicles from the table, and we'll need to create an XML Schema to reflect this. Just like JDBC maps SQL types to Java types, XMLBeans map schema types to Java types. By looking at the table's structure, you can imagine that we are dealing with only two types of data. In the Java world these types are String and int (price is an int because most automobile prices do not list cents, but we could use float as well). In the schema world, these types are also string and int. Although in our example we use simple types, XMLBeans can handle any schema type. Listing 1 shows the completed XML Schema. Notice that there are two top-level elements defined—vehicleList, which is simply a list of the second element, vehicle. The vehicle element contains node definitions for each column in our inventory table.

Now that an XML Schema has been created, it must be compiled into XMLBeans. If you are using the XMLBeans project, which can be downloaded from Apache, then this compilation can be performed through the Schema Compiler utility, or the special xmlbean Ant task. The scomp utility found in the bin directory of your XMLBeans distribution can be used to compile schemas in a variety of ways. (Refer to the usage documentation for this utility for more information.) The xbean.jar file, located in the lib directory of your XMLBeans distribution, is the container for the xmlbean Ant task. Schema compilation through the Ant task has the obvious benefit of allowing us to include the schema compilation with our other usual build tasks.




Back to top












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home