Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Use XML data binding to do your laundry

Use XML data binding to do your laundry

Tutorial Details:

Use XML data binding to do your laundry
Use XML data binding to do your laundry
By: By Sam Brodkin
Explore JAXB and Castor from the ground up
et's face it; XML by itself is just another data format that is annoying to access from your Java programs. Don't get me wrong; I appreciate XML for its portability, its separation of data and presentation, and its human and computer readability. However, I just can't be bothered writing DOM (Document Object Model), SAX (Simple API for XML), or even JDOM code to programmatically work with XML data. I have better things to do with my precious programming time.
What I need is a summer intern: someone who can get me lunch and do my dirty work for me -- like writing Java classes that correspond to every XML document type I work with. These Java classes could turn XML documents into my program's objects. Each XML tag would map to an object attribute, and a tag's contents would be the attribute value. Then, for a real challenge, I'd ask my intern to provide a marshal method in each Java class.
"Who's Marshal?" the intern might ask.
"A military officer who marshals things, arranges things in methodical order," I'd explain smugly.
Marshaling a Java object means converting it to XML format for storage or for sending. It's like when you fold your socks to put them neatly away -- which, come to think of it, is something else my intern could do. Later, when you or someone else wears those socks again, it's like turning an XML document back into useable Java objects -- unmarshaling. And you'll agree that, when you wake up in the morning, it's nice to find a basket full of neatly folded socks.
This is the classic marshal and unmarshal diagram, but for fun, this one illustrates my socks analogy.
Marshaling and unmarshaling
The ability to work with unmarshaled XML documents would be great because I could use and maintain regular Java objects much more easily and naturally than I could with a bunch of XML parsing code. My unmarshaled Java objects could even validate attributes based on the original XML Schema constraints. This validation would include type checking and verifying range values. I would have a way to programmatically construct a Java object that could save itself as an XML document valid against a certain XML Schema. Now we're talking! But what about performance? Using these unmarshaled objects, my application would be faster than SAX parsing and require less memory than DOM parsing.
Do you want to work as my intern this summer? I know what you're thinking: writing the marshal, unmarshal, and validating-accessor methods based on schema constraints would be a long and tedious task. In addition, every time I changed my XML Schema, you'd have to update this code. Doing my laundry wouldn't be much fun either. Doesn't matter; I don't want to baby-sit and entertain an intern all summer anyway. I've got better things to do!
What if I told you that there already are XML data-binding frameworks that can generate this type of marshaling and unmarshaling code for you? Just feed in a DTD (document type definition) or an XML Schema and -- presto! -- you have Java classes that can marshal, unmarshal, and check data constraints. And like many Java XML tools, these frameworks are mostly free. In this article, we'll examine two such frameworks: Sun's Java Architecture for XML Binding (JAXB) and Castor from the Exolab Group.
XML data constraints: DTD versus XML Schema
A valuable XML concept is the ability to define your own XML vocabulary. An XML vocabulary is an industry-specific XML information model or document type that you define for XML data sharing. In other words, you define constraints that specify what a particular group of XML documents should always look like. Document creators, programmers, graphic designers, and database specialists use a constrained document type as the basis for creating compatible application pieces. This parallel collaboration around a document type is easy because everyone knows ahead of time what the constrained XML documents will look like.
You can define an XML vocabulary by constraining XML in two different ways. The original method from the XML specification uses a DTD. The new and improved approach uses the recently formalized W3C (World Wide Web Consortium) Recommendation, XML Schema.
For example, in this article, I will define an XML vocabulary for describing socks. I'm a sock expert, since I wear socks almost every day, and so I know exactly what information is needed to fully describe a sock collection. Every sock in my definition has the following descriptive properties: number, name, image, color, price, and smell.
Thus, I can create the following DTD to formally describe a sock collection:
Listing 1. socks.dtd









Listing 1 says simply that an XML document conforming to my socks.dtd constraints must have zero or more socks. Each sock has exactly one name, image, color, price, and smell -- in that order. The color can only have the values white or black . Each sock has an attribute called number . Do you see why we say DTDs constrain conformant XML documents? (For more on DTDs, see Resources .)
An XML document that is valid against this DTD -- that is, one that follows the constraints correctly -- might look like this:
Listing 2. socks.xml




black socks
blacksocks.jpg

9.99
7


white socks
whitesocks.jpg

5.34
2


old white socks
oldwhitesocks.jpg

2.20
9


DTDs have garnered some complaints, especially from programmers. The problem: DTDs really constrain only the document's structure, not the data it contains. All the elements and attributes are strings, and you can't specify allowed value ranges. The best data constraining you can do with a DTD is to require that attributes be strings from a constant list. Furthermore, DTDs are not in XML format themselves, so they don't seem to fit in too well.
The answer to these deficiencies: XML Schema. XML Schemas are in XML format; they allow data typing, user-defined types, and range value constraints. XML Schema's popularity and software support is growing because it is now a final W3C Recommendation. Here you'll find an example of constraining the same document type using XML Schema:
Listing 3. socks.xsd






































0=clean and 10=smells terrible







That looks a little more complicated, but believe me, the complexity is worth it. In addition to the constraints we specified in the DTD, XML Schema lets us add the following:
The contents of the tag must end with an image type extension (like .gif ).
The must be a number with two fractional digits (like 5.34).
The must be an int between 0 and 10. There is also a documentation comment stating 0=clean and 10=smells terrible .
For more on XML Schemas, see the schema tutorial in Resources .
To associate socks.xml with our XML Schema, we'll change only some attributes in the root ( socks ) tag:
Listing 4. socks.xml for use with the XML Schema

xsi:noNamespaceSchemaLocation="socks.xsd">

black socks
...
How do we check to see if an XML document is valid against a DTD or XML Schema? We use an XML parser like Apache Xerces to verify that an XML document conforms to the socks.dtd or socks.xsd constraints. I've provided a Windows batch file in my sample code called validate.bat to do that:
validate.bat \socks.xml
This invokes the Xerces parser in validation mode, which asks it to please check socks.xml against its stated document type. Xerces now supports both DTD and XML Schema validation.
I'm a teacher, so I always like to include programming exercises. As an exercise, run validate on socks.xml , change socks.xml to make it invalid, and run validate again. Xerces should produce helpful error messages.
Onward to


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Use XML data binding to do your laundry

View Tutorial:
Use XML data binding to do your laundry

Related Tutorials:

XML JavaBeans, Part 1 - JavaWorld February 1999
XML JavaBeans, Part 1 - JavaWorld February 1999
 
XML JavaBeans, Part 2 - JavaWorld March 1999
XML JavaBeans, Part 2 - JavaWorld March 1999
 
XML for the absolute beginner - JavaWorld - April 1999
XML for the absolute beginner - JavaWorld - April 1999
 
Create a custom Java 1.2-style ClassLoader - JavaWorld March 2000
Create a custom Java 1.2-style ClassLoader - JavaWorld March 2000
 
Programming XML in Java, Part 1 - JavaWorld March 2000
Programming XML in Java, Part 1 - JavaWorld March 2000
 
Using XML and JSP together - JavaWorld March 2000
Using XML and JSP together - JavaWorld March 2000
 
Adelard, one year later - JavaWorld
Adelard, one year later - JavaWorld
 
Validation with Java and XML Schema, Part 2 - JavaWorld October 2000
Validation with Java and XML Schema, Part 2 - JavaWorld October 2000
 
The magic of Merlin - JavaWorld March 2001
The magic of Merlin - JavaWorld March 2001
 
XML messaging, Part 2 - JavaWorld June 2001
XML messaging, Part 2 - JavaWorld June 2001
 
UDDI4J lets Java do the walking
UDDI4J lets Java do the walking
 
Use XML data binding to do your laundry
Use XML data binding to do your laundry
 
J2EE or J2SE? JNDI works with both
J2EE or J2SE? JNDI works with both
 
XML glossary
XML glossary
 
Sun boosts
Sun boosts enterprise Java
 
Publish and find UDDI tModels with JAXR and WSDL
Publish and find UDDI tModels with JAXR and WSDL
 
Joott Quick Start Guide
Joott Quick Start Guide JooTemplates is a templating system to generate business documents, such as forms, mailings and reports. It is being developed with the following aims
 
XML Messaging Using JBoss
It\'s common practice to share data using FTP, but an increasingly popular alternative is to use a messaging service. As always, each approach has its own pros and cons, depending on the nature of "what to share," how easy it is to implement the technolog
 
Generating an XML Document with JAXB
In this tutorial, JAXB is used to generate Java classes from an XML Schema. An example XML document shall be created from the Java classes.
 
What is Persistence Framework?
What is Persistence Framework? What is Persistence Framework? A persistence framework moves the program data in its most natural form (in memory objects) to and from a permanent data store the database. The persistence framework manages the
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.