XML

XMLDOMNode Object

The XMLDOMNode object implements the IXMLDOMNode interface. This interface contains the following properties: attributes, baseName, childNodes, dataType, definition, firstChild, lastChild, namespaceURI, nextSibling, nodeName, nodeTypeString , nodeType, nodeTypedValue, nodeValue, ownerDocument, parentNode, parsed, prefix, previousSibling, specified, text, and xml. The methods associated with IXMLDOMNode are appendChild, clonenode, hasChildNodes, insertBefore, removeChild, replaceChild, selectNodes, selectSingleNode, transformNode, and transformNodeToObject. There are no events associated with the IXMLDOMNode interface.

Looking at these properties and methods, you can see that they're all included in the IXMLDOMDocument interface and have been defined above. The same methods exist in both interfaces because IXMLDOMNode is used as the base interface for building all W3C DOM objects except for IXMLDOMImplementation, IXMLDOMNodeList, and IXMLDOMNamedNodeMap, as illustrated in Figure 11-1.

Besides these interfaces, Internet Explorer 5 has three additional interfaces: IXTLRuntime, IXMLDOMParseError, and IXMLHTTPRequest. You can see all the interfaces, including those specific to Internet Explorer 5, in Figure 11-2.

Figure 11-1. The relationship between the W3C DOM object interfaces.

Figure 11-2. Internet Explorer 5 DOM interfaces.

Let's look at how to code some of the methods and properties that belong to the IXMLDOMNode interface.

NOTE
The following example will show you how to use some of the properties and methods of the IXMLDOMNode interface using Visual Basic. If you have access to Visual Basic, I highly recommend that you follow the examples. If you don't have Visual Basic, you can easily convert this example to a script sample. This example will print out the values of various properties to the Immediate Window.

To create the sample application, follow these steps:

  1. Open Visual Basic, create a standard EXE project, and change the name of the default form to frmDOMTest.
  2. Choose References from the Project menu, and add a reference to Microsoft XML, version 2.0.
  3. Add a command button to frmDOMTest called cmdNode with a caption Nodes.
  4. Add the following code to the click event handler of cmdNode:

  5.   Private Sub cmdNode_Click()
      Dim objXMLDoc As DOMDocument
      'Create a node object that is a reference to the root object.
      Dim objRoot As IXMLDOMNode
      'Create a node object that can be used to create a new node.
      Dim objNewNode As IXMLDOMNode
      Set objXMLDoc = New DOMDocument
      'Turn off asynchronous load as we do not need it for this example.
      objXMLDoc.async = False
      'Open the file shown below.
      objXMLDoc.Load ("c:\Books.xml")
      'The documentElement will return the root element.
      Set objRoot = objXMLDoc.documentElement
      'Begin printing out properties for the root.
      Debug.Print "XML for the root: " & vbCrLf & objRoot.xml
      Debug.Print "BaseName for the root: " & objRoot.baseName
      Debug.Print "Namespace prefix for the root: " & objRoot.prefix
      Debug.Print "DataType for the root: " & objRoot.dataType
      'We will begin to walk through the document starting at the first
      'child.
      Debug.Print "First Child XML for the root: " & vbCrLf & _
                      objRoot.firstChild.xml
      'We will get the next child, which is two elements down from
      'the root.
      Debug.Print "First Child of Child XML for the root: " & _
                      objRoot.firstChild.firstChild.xml
      'Nextsibling will return a node on the same level, in this case
      'the same level as the second element down from the root.
      Debug.Print "Second Child of Child XML for the root: " & _
                      objRoot.firstChild.firstChild.nextSibling.xml
      Debug.Print "Third Child of Child XML for the root: " & _
                      objRoot.firstChild.firstChild.nextSibling. _
                      nextSibling.xml
      Debug.Print "Fourth Child of Child XML for the root: " & _
                      objRoot.firstChild.firstChild.nextSibling. _
                      nextSibling.nextSibling.xml
      Debug.Print "Namespace URI for the root: " & _
                      objRoot.namespaceURI
      Debug.Print "Nodename for the root: " & objRoot.nodeName
      Debug.Print "NodeType for the root: " & objRoot.nodeType
      Debug.Print "NodeType String for the root: " & _
                      objRoot.nodeTypeString
      Debug.Print "NodeValue for the root: " & objRoot.nodeValue
      Debug.Print "parentNode for the root: " & _
                      objRoot.parentNode.xml
      'Using XSL to get a single node
      Debug.Print "XSL selecting first child node of the item node: " & _
           vbCrLf & objRoot.selectSingleNode("item/*").xml
      Set objNewNode = objXMLDoc.createNode(1, "test", "")
      objRoot.appendChild objNewNode
      Debug.Print "Root XML after appending: " & vbCrLf & objRoot.xml
      Set objNewNode = Nothing
      Set objRoot = Nothing
      Set objXMLDoc = Nothing
      End Sub
    

Notice that we first get a reference to the document object. Using this reference, we can get a reference to the XMLDOMNode object. Then we start navigating the nodes in the XML document. Finally, we create a node named test and append it as a child node to the root node. To test this application, let's create an XML document called Books.xml in the C:\ directory with the following XML:

  <?xml version="1.0" ?>
  <northwind:BOOKS xmlns:northwind="www.northwindtraders.com/PO">
     <item>
        <title>Number, the Language of Science</title>
        <author>Danzig</author>
        <price>5.95</price>
        <quantity>3</quantity>
     </item>
  </northwind:BOOKS>

When you run the program and click the Nodes button, the results are as follows:

  XML for the root:
  <northwind:BOOKS xmlns:acme="www.northwindtraders.com/PO">
     <item>
        <title>Number, the Language of Science</title>
        <author>Danzig</author>
        <price>5.95</price>
        <quantity>3</quantity>
     </item>
  </northwind:BOOKS>
  BaseName for the root:BOOKS
  Namespace prefix for the root:northwind
  DataType for the root:
  First Child XML for the root:
  <item>
     <title>Number, the Language of Science</title>
     <author>Danzig</author>
     <price>5.95</price>
     <quantity>3</quantity>
  </item>
  First Child of Child XML for the root: <title>Number, the Language
     of Science</title>
  Second Child of Child XML for the root: <author>Danzig</author>
  Third Child of Child XML for the root: <price>5.95</price>
  Fourth Child of Child XML for the root: <quantity>3</quantity>
  Namespace URI for the root: www.northwindtraders.com/PO
  Nodename for the root: northwind:BOOKS
  NodeType for the root: 1
  NodeType String for the root: element
  NodeValue for the root:
  parentNode for the root: <?xml version="1.0"?>
  <northwind:BOOKS xmlns:northwind="www.northwindtraders.com/PO">
     <item>
        <title>Number, the Language of Science</title>
        <author>Danzig</author>
        <price>5.95</price>
        <quantity>3</quantity>
     </item>
  </northwind:BOOKS>
  XSL selecting first child node of ITEM:
  <title>Number, the Language of Science</title>
  Root XML after appending:
  <northwind:BOOKS xmlns:northwind="www.northwindtraders.com/PO">
     <item>
        <title>Number, the Language of Science</title>
        <author>Danzig</author>
        <price>5.95</price>
        <quantity>3</quantity>
     </item>
     <test/></northwind:BOOKS>

Notice that the test element was inserted as the last child of the root, which is what we would have expected. Once this element is inserted, you can add text values or make other changes.

NOTE
Though we have not discussed XSL yet, we used XSL to get a single node in the previous application. XSL defines the location of elements using the XPath syntax. We'll discuss XSL in detail in Chapter 12. In this chapter, we will use the XPath syntax with the selectSingleNode method.

Several methods and properties belonging to the document object will return other objects in the hierarchy, such as selectNodes or attributes. We'll discuss these methods and properties while examining other object interfaces in the XML DOM.