XML and Java technology: Data binding in 1,000 varieties  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews XML
Written by Brett D. McLaughlin, Sr.   
Tuesday, 13 March 2007

{mos_sb_discuss:24} 

Beyond using XML as a simple data format, data binding is one of the most popular uses of XML. It allows even beginner programmers to work with XML in a native programming language, and in many cases doesn't require any XML expertise at all.



This article doesn't present a solution, instead Brett introduces some discussion topics to start you thinking about how you use XML and data binding. You're encouraged to share your thoughts with others on the XML and Java technology discussion forum.  

XML for business rather than technology

As XML has become increasingly popular, more and more emphasis is put on usability. In other words, programmers and managers want to treat XML less as a technology, with all its own semantics and lexical constructs, and more like pure data, accessed without having to worry much about the specifics of the XML data format.

Arguably the easiest way to achieve this move from using XML as a technology to a business format is data binding. Data binding is when the data in an XML document is manipulated using an Application Programming Interface (API) that doesn't force the programmer to know much about XML, work with angle brackets, or think about things like CDATA sections or entity references. But even when it comes to data binding, you'll find lots of options, and plenty of important issues to think closely about before you charge ahead.

For the purposes of this discussion, I'll take on two basic issues related to data binding:

  1. The way that data is represented through the data binding API.
  2. The usage of the data once its treated as business data.

In the most generic case, data binding means that the data in an XML document is turned into an object in the programming language being used.

Object-based APIs for data binding

For example, take the following XML fragment:

<person>
 <firstName>Brett</firstName>
 <lastName>McLaughlin</lastName>
 <email> This e-mail address is being protected from spam bots, you need JavaScript enabled to view it </email>
</person>

You might turn this into an object, say in Java™ code, that is an instance of a Person class, with member variables firstName, lastName, and email. The instance would be filled with the data from the fragment, and you could access that data with method calls like myPerson.getFirstName().

Document-based APIs for data binding

While that's the most common approach to data binding, APIs that take an XML document and represent the entire document as an object are also a form of data binding. These APIs include the Document Object Model (DOM), JDOM, and dom4j, all of which create an object model of an XML document in Java coding.

In these models, you'd make calls like rootElement.getChild("firstName").getValue() (or something similar, depending on the specifics of the API). While this does require some basic knowledge of XML -- understanding what an element is and the basic structure of the document -- it still abstracts details about parsing from the programmer. That's the essence of data binding: being able to worry about the data more than the format that presents the data.

Easier for the programmer (sort of)

Once you move to a more common data binding solution, like Sun's JAXB, you have even less underlying XML syntax to worry about. You really can work entirely with Java (or your favorite programming language) objects and methods and variables. Even details about elements and the structure of the document become hidden in the objects created by the data binding process.

The key here, though -- and what is often not thought about -- is that you still have to either match your XML data structure to objects in your system, or create objects in your system that match the format of XML data that you work with. In both cases, the mapping to XML is a little less obvious, but it's still a part of the process.


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Sunday, 08 July 2007 )
 
< Prev   Next >