What is XML Data Binding?

This section provides an overview of XML Data Binding. It covers what it is, and the practical applications that you can put it to.
If you are new to or unsure of what XML data binding is then this is the place to start.

Part 1 - What is XML Data Binding? - an introduction to the technology.
Part 2 - The Product - a summary of the product.
Part 3 - In Detail - a more in depth look at what is produced when code is generated from a schema.

Before we start lets just ensure we understand a few concepts/technologies

XML - eXtensible Markup Language.

XML is a text based file format, it allows data to be stored in a tree (hierarchy). Each piece of data is stored along with a label (tag), so it is easy for people to read and understand.

XSD - XML Schema Definition

A schema is a formal description of what can and can not go into a given XML document (Just like a database schema). XSD is just one of several schema specifications, but it is the most widely adopted. The XSD standard has evolved over a number of years, and is controlled by the W3C. For some reason, it has become one of the most complex internet standards. Because of this producing code that complies correctly with a standard is a lot more complex than it may first appear. Just one of the reasons XML Data Binding is gaining in popularity.

DOM Parser - Document Object Model

A DOM parser allows a program to understand an XML document, it reads in the XML, and represents it in memory as a tree of nodes.

The tree produced from the parser is verbose, and generic. Interpreting this tree requires detailed knowledge of the schema. As a result a lot of code has to be written to create or interpret the content of even a simple DOM tree. Furthermore the developer doing the coding, needs a very good understanding of the XSD standard (a 400 pages standard, which is not easy reading).

Once you start adding more complex schema concepts such as namespaces extension's, substitution groups etc. It can be very tricky to work with, often consuming a large portion a projects development time.

XML Data Binding

Using the XSD (the document description) a code library is generated (set of classes). This library makes it simple to manipulate XML documents described by the XSD.

Manipulating your XML documents through this library changes the nature of the task, moving from the manipulation of a generic unstructured tree of nodes (DOM). To a hierarchy of objects specifically designed to deal with the data you are working with (Strongly typed XML Data Binding Library). It stops the the round peg in the square hole problem, basically using the DOM approach you can create any kind of XML document you want, you can put elements & attributes anywhere (this is a bad thing, putting even a single element/attribute in the wrong place invalidates the resulting document). Using the generated library you can only put elements and attributes in the correct places. The library is in effect the a guide making it impossible to put an element in the wrong place. Not only this but it makes it much simpler to build up and read in XML documents.

In sort it cuts your development effort, simplifies maintenance, increases reliability and thus saves your project time and money throughout its life cycle.

Still not clear, well lets look at the benefits.

A Simple Example Lets 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)

(or depicted graphically, Figure 1b)

The obvious way to do this is using a DOM. Ill 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 lets see the same code written using an XML Data Binding library, see Figure 4.
(We'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 its much simpler to maintain. 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 ?
Well its generated for a tool. There are a number of different tools out there at the moment, and each goes about the job of turning an XML schema into an object orientated library a little differently. We will look at these in more detail in part 2

The Generated Library

The library used in the sample (Figure 4), is a simple strongly typed hierarchy of objects, the structure of this can be seen in the UML diagram (Figure 5)

(figure5)

The generated Source Code

The generated C# sample application comes complete with a simple viewer that makes it possible to see the structure & properties of the generated XML Data Binding Library. Using the viewer you can also edit the properties and see the effect that this has on the generated XML.