What is XML Data Binding - Tutorial - Part 1/3 What is XML Data Binding - Tutorial - Part 1/3

Home > XML Tutorials > Part 1 - What is XML Data Binding?

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.

XML Data Binding Overview

XML Data Binding makes it possible to read and write XML data using a programming language (like C++, C#, Java) using a class library specifically created for a given XML data format.

Before we start breaking that down, lets 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 as well as machines.

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. It's a pretty complex standard, running into about 400 pages. Because of this producing XML schemas can be difficult without a XML Schema Editor, and producing documents that comply correctly with an XML Schema can be more complex than it may first appear. Just one of the reasons XML Data Binding has gained popularity.

DOM Parser - Document Object Model

A DOM parser allows a program to understand an XML document, the XML Data is parsed and interpreted as a tree of nodes.

On the right is the DOM tree resulting from the following XML data.

<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> 

The tree produced from the parser is verbose, and generic. Interpreting this tree requires detailed knowledge of the XML documents structure. 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 page technical spec). Once you start adding more complex schema concepts such as namespaces, extension's, restrictions, substitutionGroups etc. It can be very tricky to work with, often consuming a large portion a projects development time.

XML Data Binding

Using the XML Schema (the document description) a code library is generated (set of classes or an API). This library is specific to the XML schema used to build it and makes it simple to manipulate XML documents in the format described by the Schema.

Manipulating XML documents through this type of library changes the nature of the task. Moving from the manipulation of a generic unstructured tree of nodes (DOM). To a hierarchy of strongly typed objects specifically designed to deal with the data described in the XML Schema. It stops 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 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 short it cuts your development effort, simplifies maintenance, increases reliability and thus saves your project time and money throughout its life cycle.

An XML Data Binding Example

This simple example will show how to construct an XML document using a DOM Model and then again using and XML Data Binding library.

Lets assume the following XML Schema describes our XML document.


<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> 

First lets create an XML document that conforms to the schema in Figure 1 using the DOM model (in this case we are using Visual Basic, but the code would be comparable in C#, Java or even C++).

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
                

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> 

Now lets see the same code written using an XML Data Binding library, see Figure 4.

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

So, just a bit less code, and its much simpler, easy to understand and thus maintain. The advantages can be even more obvious when looking at reading an XML documents.

So where does the XML Data Binding library BookStoreSampleLib come from?

Well its generated by an XML Data Binding tool, XML Data Binder. This examines the definitions in the XML Schema and creates an Object Oriented Class library that represents the structure described in the XSD Schema.

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

Liquid XML Data Binder also generates a sample application along with the library, this can be used to examine the object model that represents a given XML data file, or it can be used to generated the source code required to create a given XML document.

Try all the features of Liquid Studio Download Free Trial Now

More Editors and Tools

FEATURE DETAILS XML Data Binder

XML Data Binder

FEATURE DETAILS Graphical XSD Editor

Graphical XML Schema Editor(XSD)

FEATURE DETAILS XML Data Binding Editions

XML Data Binding Editions