XML Data Binding

XML Data Binding

Contents

  • What Is XML Data Binding?: An introduction to the technology.
  • Products: A summary of the products currently available
  • Functional Summary: A summary of what these products are capable of
  • In Detail: A more in-depth look at what is produced when code is generated from a schema

What Is XML Data Binding?

XML Data Binding allows you to manipulate an XML document via a set of simple objects. The rules defining the ‘shape’ of the XML document are described in an XML schema. Typically, it is possible to read an XML document into an XML binding library and manipulate it programmatically via simple get and set methods. Conversely, a document can be created from an XML data binding library, and serialized as an XML document.

Still not clear? Well, look at the benefits.

A Simple Example

Try to create an XML document that conforms to the schema in Figure 1a.

<xsd:schema xmlns_xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
   <xsd:element name="bookstore" type="bookstoreType" />
   <xsd:complexType name="bookstoreType">
      <xsd:sequence maxOccurs="unbounded">
         <xsd:element name="book" type="bookType" />
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="bookType">
      <xsd:sequence>
         <xsd:element name="title"  type="xsd:string" />
         <xsd:element name="author" type="authorName" />
         <xsd:element name="price"  type="xsd:decimal" />
      </xsd:sequence>
      <xsd:attribute name="genre"           type="xsd:string" />
      <xsd:attribute name="publicationdate" type="xsd:string" />
      <xsd:attribute name="ISBN"            type="xsd:string" />
   </xsd:complexType>
   <xsd:complexType name="authorName">
      <xsd:sequence>
         <xsd:element name="first-name" type="xsd:string" />
         <xsd:element name="last-name"  type="xsd:string" />
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

Figure 1a

To see a graphic depiction, see Figure 1b.

The obvious way to do this is by using a DOM. I’ll use MSXML and VB6 as an example; see Figure 2.

Dim oXmlDoc As MSXML2.DOMDocument40
Dim oElmBookStore As MSXML2.IXMLDOMElement
Dim oElmBook As MSXML2.IXMLDOMElement
Dim oAttrGenre As MSXML2.IXMLDOMAttribute
Dim oAttrPublicationDate As MSXML2.IXMLDOMAttribute
Dim oAttrISBN As MSXML2.IXMLDOMAttribute
Dim oElmBookTitle As MSXML2.IXMLDOMElement
Dim oElmBookAuthor As MSXML2.IXMLDOMElement
Dim oElmBookAuthorFirstName As MSXML2.IXMLDOMElement
Dim oElmBookAuthorLastName As MSXML2.IXMLDOMElement
Dim oElmBookPrice As MSXML2.IXMLDOMElement

' create the document
Set oXmlDoc = New MSXML2.DOMDocument40

' Create the document element
Set oElmBookStore = oXmlDoc.createElement("bookstore")
oXmlDoc.appendChild oElmBookStore

' Add the first book
Set oElmBook = oXmlDoc.createElement("book")
oElmBookStore.appendChild oElmBook

' add genre attribute
Set oAttrGenre = oXmlDoc.createAttribute("genre")
oElmBook.Attributes.setNamedItem oAttrGenre
oAttrGenre.Value = "autobiography"

' add publicationdate attribute
Set oAttrPublicationDate =
   oXmlDoc.createAttribute("publicationdate")
oElmBook.Attributes.setNamedItem oAttrPublicationDate
oAttrPublicationDate.Value = "1981"

' add publicationdate attribute
Set oAttrISBN = oXmlDoc.createAttribute("ISBN")
oElmBook.Attributes.setNamedItem oAttrISBN
oAttrISBN.Value = "1-861003-11-0"

' Add Title to book
Set oElmBookTitle = oXmlDoc.createElement("title")
oElmBook.appendChild oElmBookTitle
oElmBookTitle.nodeTypedValue = "The Autobiography of Benjamin _
                                Franklin"

' Add Author to book
Set oElmBookAuthor = oXmlDoc.createElement("author")
oElmBook.appendChild oElmBookAuthor

' Add the first name attributes to the author
Set oElmBookAuthorFirstName = oXmlDoc.createElement("first-name")
oElmBookAuthor.appendChild oElmBookAuthorFirstName
oElmBookAuthorFirstName.nodeTypedValue = "Benjamin"

' Add the last name attributes to the author
Set oElmBookAuthorLastName = oXmlDoc.createElement("last-name")
oElmBookAuthor.appendChild oElmBookAuthorLastName
oElmBookAuthorLastName.nodeTypedValue = "Franklin"

' Add Price to book
Set oElmBookPrice = oXmlDoc.createElement("price")
oElmBook.appendChild oElmBookPrice
oElmBookPrice.nodeTypedValue = "8.99"

' output the XML we created
Debug.Print oXmlDoc.xml

Figure 2

The code in Figure 2 creates the XML Document in Figure 3. Lots of code to do so little!

<bookstore>
   <book genre="autobiography" publicationdate="1981"
         ISBN="1-861003-11-0">
      <title>The Autobiography of Benjamin Franklin</title>
      <author>
         <first-name>Benjamin</first-name>
         <last-name>Franklin</last-name>
      </author>
      <price>8.99</price>
   </book>
</bookstore>

Figure 3

Now, see the same code written by using an XML Data Binding library (see Figure 4). I’ll cover the details of where the XML Binding library comes from later.

Dim oElmBookStore As New BookStoreSampleLib.Bookstore
Dim oElmBook As BookStoreSampleLib.Book

' create a new book
Set oElmBook = oElmBookStore.Books.Add

' populate the book
oElmBook.Genre            = "autobiography"
oElmBook.PublicationDate  = "1981"
oElmBook.ISBN             = "1-861003-11-0"
oElmBook.Title            = "The Autobiography of Benjamin Franklin"
oElmBook.Author.Firstname = "Benjamin"
oElmBook.Author.Lastname  = "Franklin"
oElmBook.Price            = 8.99

' output the XML we created
Debug.Print oElmBook.xml

Figure 4

Just a bit less code, and it’s much simpler to maintain. And, the advantages can be even more obvious when looking at reading in and interpreting XML documents.

So, where does the XML data binding library BookStoreSampleLib come from? A tool generates it for you. There are a number of different tools on the market at the moment, and each goes about the job of turning an XML schema into an object orientated library a little differently.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read