- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
These days, many programmers are used to programming with objects; they're familiar with concepts such as classes, methods, parameters, and so on. In some cases, this familiarity can lead to resistance on the part of the programmer to use XML, because it doesn't seem to readily adapt itself to the thought processes involved in creating an object-oriented application. Data binding is the process of making data act like an object so that it can be manipulated like an object. Consider, for example, our sample order file:
<?xml version="1.0"?> <order orderid="THX1138" customerNumber="3263827"> <lineitem itemid="C33"> <item>3/4" Hex Bolt</item> <quantity>36</quantity> <unitprice currency="dollars">.35</unitprice> </lineitem> <lineitem itemid="M48"> <item>Condenser</item> <quantity>1</quantity> <unitprice currency="dollars">2200</unitprice> </lineitem> </order>
If the order were an object, it might be an object of type Order, with properties such as orderid and customerNumber, both of which might be string objects, and a lineitem property that might consist of a group of Lineitem objects. The Lineitem class would define properties such as itemid, item, quantity, and unitprice.
If you were designing the Order class, you'd probably have methods such as:
public java.lang.String getOrderid(); public void setOrderid(java.lang.String orderid); public java.lang.String getCustomerNumber(); public void setCustomerNumber(java.lang.String customerNumber); public Lineitem[] getLineitem(); public Lineitem getLineitem(int index); public int getLineitemCount(); public void setLineitem(Lineitem[] lineitemArray); public void setLineitem(int index, Lineitem vLineitem); public void addLineitem(Lineitem vLineitem); public void removeAllLineitem();
With these methods, you could control the information stored in the object.
The existance of XML Data Binding tools means that this way of working doesn't have to be mutually exclusive with XML. Instead, you can use a tool that analyzes a DTD or XML Schema and uses the information to automatically generate the necessary classes and the methods necessary for obtaining and manipulating the data.
More than that, though, these tools include the ability to marshal and unmarshal XML data. Marshalling is the process if taking data that's stored in an object and saving it back out as XML. Unmarshalling is the process of reading in the XML file and using it to populate the object. For example, we can create a simple program that creates an order and adds a line item to it:
import java.io.FileWriter; public class TakeAnOrder { public static void main (String args[]){ try{ Order order = new Order(); order.setOrderid("THX1138"); order.setCustomerNumber("3263827"); Lineitem lineitem = new Lineitem(); lineitem.setItem("3/4\" Hex Bolt"); lineitem.setItemid("C33"); lineitem.setQuantity("36"); Unitprice unitprice = new Unitprice(); unitprice.setContent(".35"); unitprice.setCurrency("dollars"); lineitem.setUnitprice(unitprice); order.addLineitem(lineitem); FileWriter writer = new FileWriter("neworder.xml"); order.marshal(writer); } catch (Exception e){ e.printStackTrace(); } } }
(I'm using Java here, but data binding tools exist for most programming languages.)
Here we create the new Order object (from a class automatically generated with a tool such as Castor), and then set the orderid and customerNumber properties. We can then create a new Lineitem (again automatically generated, along with its methods), populate it, and add it to the Order. Once it's been created and populated, we can marshal, or save, the data out to a file.
Now, notice that all of this deals with the content of the XML file, but not its structure. This process is designed to be completely independant of the structure of the original file. In fact, running the application as-is resulted in the following output:
<?xml version="1.0" encoding="UTF-8"?> <order lineitem-count="1" valid="true"> <lineitem valid="true"> <unitprice valid="true"> <currency>dollars</currency> <content>.35</content> </unitprice> <item>3/4" Hex Bolt</item> <itemid>C33</itemid> <quantity>36</quantity> </lineitem> <customer-number>3263827</customer-number> <orderid>THX1138</orderid> </order>
Now, I've cleaned it up a little bit so you can see the data, but it's obvious that while all of the data is there, it's barely recognizable from our original document. That's where mapping comes in.
Mapping enables you to tell the application how to handle the information stored in the object. For example, the mapping file:
<?xml version="1.0"?> <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://castor.exolab.org/mapping.dtd"> <mapping> <class name="Order"> <map-to xml="order"/> <field name="orderid" type="java.lang.String"> <bind-xml name="orderid" node="attribute"/> </field> <field name="customerNumber" type="java.lang.String"> <bind-xml name="customerNumber" node="attribute"/> </field> <field name="lineitem" type="Lineitem"> <bind-xml name="lineitem" node="element"/> </field> </class> <class name="Lineitem"> <map-to xml="lineitem"/> <field name="itemid" type="java.lang.String"> <bind-xml name="itemid" node="attribute"/> </field> <field name="item" type="java.lang.String"> <bind-xml name="item" node="element"/> </field> <field name="quantity" type="java.lang.String"> <bind-xml name="quantity" node="element"/> </field> <field name="unitprice" type="Unitprice"> <bind-xml name="unitprice" node="element"/> </field> </class> <class name="Unitprice"> <map-to xml="unitprice"/> <field name="currency" type="java.lang.String"> <bind-xml name="currency" node="attribute"/> </field> <field name="content" type="java.lang.String"> <bind-xml node="text"/> </field> </class> </mapping>
tells the application that properties such as itemid and currency should be treated as attributes rather than element children, and that the content of the unitprice is actually the text, and not a property named content.
By using a mapping file, you can create an application that is completely independent of the structure of the actual XML data. The only difference is that instead of marshalling the data directly from the order object, you can use a Marshaller object that can read the mapping and use it appropriately:
... lineitem.setUnitprice(unitprice); order.addLineitem(lineitem); FileWriter writer = new FileWriter("neworder.xml"); Mapping mapping = new Mapping(); mapping.loadMapping("mapping.xml"); Marshaller marshaller = new Marshaller(writer); marshaller.setMapping(mapping); marshaller.marshal( order ); } catch (Exception e){ e.printStackTrace(); } } }
Overall, the idea of XML data binding is that of working with XML data as though it's an object. Here we're just creating an object from scratch, but you can also build applications that unmarshal the data, or read it from a file, and then update it and put it back.