This section provides an overview of XML Data Binding. It covers what it is, and the practical applications that you can put it to. Part 1 - What is XML Data Binding? - an introduction to the technology. Before we start lets just ensure we understand a few concepts/technologies 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
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 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.DOMDocument40Dim 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. 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 ? 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.
|