21 October 2006

Working with XML - Part 1 - Using the DOM

XML (Extensible Markup Language) is a way of storing information as text by applying structure and meaning to data using a system of nested elements and attributes. HTML is a loose form of XML because it consists of elements, attributes and text although it doesn't always obey the strict rules necesary for valid XML.

The basic rules are:

  • Element and attribute names are case sensitive i.e. derek != Derek
  • Each element must be closed
  • All an element's child elements must be closed before it can be
  • An XML document must have one element only as its root node

Document Type Definitions (DTD) and XML Schema Definition (XSD) are methods for defining the structure of an XML document and ensuring it adheres to your specification. Although i'm not going to cover them in this post they're very important particularly if you're letting other people write XML for your system.

Examples are in ASP/VBScript

Example 1 - Some XML

<?xml version="1.0" ?>
<library>
   <authors>
      <author id="12345">
         <name>Charles Dickens</name>
      </author>
      <author id="23456">   
         <name>Rudyard Kipling</name>
      </author>         
   </authors>
   <books>
      <book>
         <title>Great Expectations</title>
         <author>12345</author>
      </book>
      <book>
         <title>The Jungle Book</title>
         <author>23456</author>
      </book>
   </books>
</library>

In this example we have a library element as the root node of the XML document. Inside that we have an authors element containing author elements and a books element containing book elements. The author elements have an id attribute and a name child element whereas the book elements have a title element and an author element containing the id of the associated author.

You can see from this example how it's easy to denote quite complex relationships in a simple, readable manner. I'll base the other examples in this post around this bit of XML

Example 2 - Loading XML in to a DOM

Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load(Server.MapPath("mydoc.xml"))
Response.Write xmlDoc.xml

This snipet loads and XML document from a file into a DOM object. There's also a LoadXML function on the Microsoft DOM object for loading a string containing XML.

Once we've got our XML loaded we can traverse the tree, read data, change properties and save it back to a file.

Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load(Server.MapPath("mydoc.xml"))
Set ndRoot = xmlDoc.documentElement

'retrieving element names
Response.Write ndRoot.tagName & "<br />"

'looping though nodes
Set ndAuthors = ndRoot.firstChild
For Each ndAuthor In ndAuthors.childNodes
   Response.Write ndAuthor.getAttribute("id") & "<br />"
Next

'setting attributes
Set ndSecondAuthor = ndAuthors.childNodes(1)
ndSecondAuthor.setAttribute "id", 99999
Response.Write ndSecondAuthor.getAttribute("id") & "<br />"

'retrieving and setting node text
Response.Write ndSecondAuthor.firstChild.text & "<br />"
ndSecondAuthor.firstChild.text = "Joe Bloggs"
Response.Write ndSecondAuthor.firstChild.text & "<br />"

Links

No comments: