XML

XMLDOMDocumentFragment Object

The XMLDOMDocumentFragment object will be used to create fragments of documents that can be appended to another document. When the XMLDOMDocumentFragment object is inserted into a document object, the root node of the XMLDOMDocumentFragment is not inserted, only its children. Thus, XMLDOMDocumentFragment objects are useful for inserting child elements into a document.

The XMLDOMDocumentFragment object implements the IXMLDOMDocumentFragment interface. This interface inherits all the IXMLDOMNode interface's methods and properties, but it doesn't extend the interface, so this interface has no additional methods or properties of its own.

XMLDOMElement Object

The XMLDOMElement object contains the elements in the document and is the most common node. The text nodes belonging to an element object contain the content of the element. If there is no text content, the XMLDOMText object will be null. This object implements the IXMLDOMElement interface. When working with the IXMLDOMElement interface, you must know beforehand what the names of the elements and attributes are that you want to retrieve and place them in the code. This is because the IXMLDOMElement interface sets and retrieves attributes and elements by their names.

In addition to the methods and properties of the IXMLDOMNode interface, the IXMLDOMElement interface has the following extended property and methods:

Extended IXMLDOMElement Property

Name Description
tagName Sets or returns the name of the element

Extended IXMLDOMElement Methods

Name Description
getAttribute (attributeName) Returns the value of the attribute with the specified attributeName.
getAttributeNode (attributeName) Returns the attribute node object with the specified attributeName.
getElementsByTagName (elementName) Returns an XMLDOMNodeList object that contains all the descendant elements named elementName.
normalize() Combines the adjacent text nodes into one unified text node. Normalizes all descendant text nodes of the element.
removeAttribute (attributeName) Removes the value of the attribute named attributeName.
removeAttributeNode (attributeNode) Removes the attribute node named attributeNode and returns the node. If there is a default value in the schema or DTD, a new attribute node will be created with the default value.
setAttribute (attributeName, newValue) Sets the attribute node named attributeName to the value newValue.
setAttributeNode (attributeName) Adds a new attribute node to the element. An existing attribute node by the same name will be replaced.

You can get a reference to an IXMLDOMElement interface by using the selectNodes method and XSL. You will now create an example to select a single element node. To do so, follow these steps:

  1. Add another command button to the frmDOMTest form with the name cmdElement and the caption Element.
  2. Insert the following code into the click event handler of this button:

  3.   Private Sub cmdElement_Click()
      Dim objXMLDoc As DOMDocument
      Dim objElement As IXMLDOMElement
      Set objXMLDoc = New DOMDocument
      objXMLDoc.async = False
      objXMLDoc.Load ("c:\Books.xml")
      Set objElement = objXMLDoc.selectNodes("//item/*").Item(1)
      Debug.Print objElement.xml
      Set objElement = Nothing
      Set objXMLDoc = Nothing
      End Sub
    

This example application selects the second child node of the item element. Then it retrieves the entire XML content of this node by using the xml property. When you run this example and click the Element button, the result is as follows:

  <author>Danzig</author>

You can get a reference to the XMLDOMAttribute, XMLDOMEntity, XMLDOMEntityReference, XMLDOMNotation, XMLDOMCharacterData, XMLDOMText, XMLDOMCDATASection, XMLDOMComment, and XMLDOMProcessingInstruction by using XSL. You can get references to these node objects just as we used the XSL statement "//item/*" to get references to the node item in the previous application. (We will discuss the XSL syntax in detail in Chapter 12. ) So in the following sections, we'll examine these interfaces without demonstrating how to use them in the applications.