Introduction XML DOM

XML documents can be viewed as tree structured documents with a root node at the origin. The document consists of many nodes. Document Object Model provides a standardize way to manipulate XML documents. Presently, Internet Explorer 5+ provides extensive support for DOM. Netscape Navigator 6 also provides full support to W3C DOM.

DOM consists of many objects which offer methods and properties to manipulate XML document, however we will consider following most common objects here.

  • XMLDOMDocument : represents entire XML document
  • XMLDOMNode : represents single node from entire document
  • XMLDOMNodeList : represents collection of nodes
  • XMLDOMNamedNodeMap : represents collection of attributes
  • XMLDOMParseError : gives errors occurred while parsing

Note : All the examples assume that you are using Microsoft XML parser. Which gets installed with IE5+ and is also available as separate download.All code fragments are developed for use in VB using MSXML2.

Using XMLDOMDocument Object

This object represents XML document and provides document level methods. This object is responsible for parsing and loading of document.

Task Example
Declaring variable to hold XML document Dim mydoc As New MSXML2.DOMDocument
Loading XML document from disk mydoc.Load ("xml_file_path")
Loading XML from string mydoc.LoadXML ("string_containing_xml")
Loading document asynchronously mydoc.async=true/false
Checking status of loading mydoc.ReadyState=[1-4]
1 - loading
2 - loaded
3 - objects partly available but read only
4 - completed
Saving document to disk "path" Response (for ASP)
Get DTD name on which the document is built mydoc.DocType
Get document URL mydoc.URL
Get underlaying XML mydoc.xml
Get root node root=mydoc.documentElement
Get a node object by its ID node=mydoc.nodeFromID("element_id")
Get node list matching a tag nodelist=mydoc.getElementsByTagName("tag")
Creating new elements* myele=mydoc.createElement("element_name")
Creating new attribute* myattb=mydoc.createAttribute("attb_name")
Creating a text node* mytxtnode=mydoc.createTextNode("text_data")
* The newly created elements must be appended to respective nodes

Using XMLDOMParseError object

This object is used to get information about errors occurred during loading of document. This object is used as soon as document gets loaded.

Task Example
Get error object myobj=mydoc.parseError
Get error code myobj.errorCode
Get reason for error myobj.reason
Get line no. at which error occurred myobj.line

Using XMLDOMNode object

This object represents a single node from XML document. Since XML documents are hierarchical in nature a node can further have child node objects. You can access childNodes collection as follows :

Dim node as object

For each node in mydoc.childNodes
' do something here

Task Example
Check if a node has child nodes node.hasChildNode
Get name of the node node.nodeName
Get node type node.nodeType
Common types are :
1 - element
2 - attribute
3 - text
Get value from the node node.nodeValue
Get XML source for node under consideration node.xml
Get parent node node.ParentNode
Get extreme child nodes node.firstChild
Get nodes on either side node.previousSibling
Adding child node node.appendChild(node_obj) - see document object
Remove child node.removeChild(obj)
Replace a child node node.replaceChild(new_obj,old_obj)
Insert a child node node.insertBefore(new_obj,exsisting_obj)

Using XMLDOMNodeList object

This object represents collection of nodes(0 based). For example, mydoc.childNodes is actually a NodeList object. You can use it as follows :

Dim nodelist as object
set nodelist=mydoc.childNodes

Task Example
Get no. of items in the node list nodelist.length
Go to next node nodelist.nextNode
Returns null on failure. Can be used in loops
Move pointer to initial position so that collection can be re-iterated nodelist.reset

Using XMLDOMNamedNodeMap object

This object represents a collection of attributes of an element. You can obtain a reference to it using following code :

dim map as object
set map=myelement.attributes

Task Example
Navigate attribute list same as node object
Get attribute value map.getNamedItem("attb").nodeValue
Create new attribute map.setNamedItem(attb_obj)
See document object for createAttribute method
Remove an attribute map.removeNamedItem("attb_name")

Some Code samples

If you are using any scripting language you can not declare variables with exact types like IXMLDOMNode. In such cases you have to use variant or object types.

  • Using DOMDocument object

    Dim mydoc As New MSXML2.DOMDocument
    Dim myerr As Object
    Private Sub Form_Load()
        mydoc.Load (App.Path + "\" + "catalog.xml")
        Set myerr = mydoc.parseError
        If myerr.errorCode <> 0 Then
            MsgBox myerr.reason
        End If
    End Sub
  • Using childNodes collection

    Dim o As MSXML2.IXMLDOMNode
    MsgBox mydoc.childNodes.length
    For Each o In mydoc.childNodes
         MsgBox "Node Text :" + o.Text
         MsgBox "Node Type :" & o.nodeType
  • Retrieving specific nodes

    Dim o As Object
    Dim temp As MSXML2.IXMLDOMNode
    Set o = mydoc.getElementsByTagName("book")
    For Each temp In o
        MsgBox temp.childNodes(1).nodeName
        MsgBox temp.childNodes(1).childNodes(0).nodeValue
  • Creating text nodes

    Dim temp As MSXML2.IXMLDOMElement
    Dim temp1 As MSXML2.IXMLDOMNode
    Dim temp2 As MSXML2.IXMLDOMNode
    Set temp = mydoc.createElement("book")
    temp.setAttribute "isbn", "400"
    Set temp1 = mydoc.createTextNode("title")
    temp1.nodeValue = "Title 4"
    Set temp2 = mydoc.createTextNode("author")
    temp2.nodeValue = "Author 4"
    temp.appendChild temp1
    temp.appendChild temp2
    mydoc.documentElement.appendChild temp
    MsgBox mydoc.xml
  • Using Namednodemap

    Dim mydoc As MSXML2.DOMDocument
    Dim oAttb As MSXML2.IXMLDOMNamedNodeMap
    Dim oNodes As MSXML2.IXMLDOMNodeList
    Dim temp As Object
    Dim Price As Currency
    Set mydoc = New MSXML2.DOMDocument
    mydoc.Load "catalog.xml"
    Set oNodes = mydoc.getElementsByTagName("book")
    For Each temp In oNodes
        Set oAttb = temp.Attributes
            If oAttb.getNamedItem("catagory").nodeValue = "prog" Then
                Price = Price + oAttb.getNamedItem("price").nodeValue
                MsgBox " Book Title :" & temp.childNodes(0).childNodes(0).nodeValue & vbCrLf & "Author :" &              temp.childNodes(1).childNodes(0).nodeValue
            End If
    MsgBox "Total price of programming books is " & "$" & Price

Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 18 December 2000

Tags : XML