Source: DynamDOM.frm

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Source: DynamDOM.frm

This sample application creates a simple, but complete, XML DOM object, with <root> as the document element. This element contains three child elements: <node1>, <node2>, and <node3>. The first child element contains character data. The second child element contains a CDATA section. The last child element contains three empty child elements: <subnode1>, <subnode2>, and <subnode3>.

Visual Basic Source File (DynamDOM.frm)

Private Function CreateDOM()
    Dim dom
    Set dom = New DOMDocument50
    dom.async = False
    dom.validateOnParse = False
    dom.resolveExternals = False
    dom.preserveWhiteSpace = True
    Set CreateDOM = dom
End Function

Private Sub Form_Load()
    Dim dom, node, attr

    On Error GoTo ErrorHandler

    Set dom = CreateDOM
    
    ' Create a processing instruction targeted for xml.
    Set node = dom.createProcessingInstruction("xml", "version='1.0'")
    dom.appendChild node
    Set node = Nothing
    
    ' Create a processing instruction targeted for xml-stylesheet.
    Set node = dom.createProcessingInstruction("xml-stylesheet", _
                                "type='text/xml' href='test.xsl'")
    dom.appendChild node
    Set node = Nothing
    
    ' Create a comment for the document.
    Set node = dom.createComment("sample xml file created using XML DOM object.")
    dom.appendChild node
    Set node = Nothing
    
    ' Create the root element.
    Dim root
    Set root = dom.createElement("root")
    
    ' Create a "created" attribute for the root element and
    ' assign the "using dom" character data as the attribute value.
    Set attr = dom.createAttribute("created")
    attr.Value = "using dom"
    root.setAttributeNode attr
    Set attr = Nothing
    
    ' Add the root element to the DOM instance.
    dom.appendChild root
    ' Insert a newline + tab.
    root.appendChild dom.createTextNode(vbNewLine + vbTab)
    ' Create and add more nodes to the root element just created.
    ' Create a text element.
    Set node = dom.createElement("node1")
    node.Text = "some character data"
    ' Add text node to the root element.
    root.appendChild node
    Set node = Nothing
      ' Add a newline plus tab.
    root.appendChild dom.createTextNode(vbNewLine + vbTab)
    
    ' Create an element to hold a CDATA section.
    Set node = dom.createElement("node2")
    Set cd = dom.createCDATASection("<some mark-up text>")
    node.appendChild cd
    Set cd = Nothing
    dom.documentElement.appendChild node
    Set node = Nothing
      ' Add a newline plus tab.
    root.appendChild dom.createTextNode(vbNewLine + vbTab)
    
    ' Create an element to hold three empty subelements.
    Set node = dom.createElement("node3")
    
    ' Create a document fragment to be added to node3.
    Set frag = dom.createDocumentFragment
        ' Add a newline + tab + tab.
    frag.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
    frag.appendChild dom.createElement("subNode1")
       ' Add a newline + tab + tab.
    frag.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
    frag.appendChild dom.createElement("subNode2")
       ' Add a newline + tab + tab.
    frag.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
    frag.appendChild dom.createElement("subNode3")
       ' Add a newline + tab.
    frag.appendChild dom.createTextNode(vbNewLine + vbTab)
    node.appendChild frag
    Set frag = Nothing
    
    root.appendChild node
       ' Add a newline.
    root.appendChild dom.createTextNode(vbNewLine)
    Set node = Nothing
    
    ' Save the XML document to a file.
    dom.save App.Path + "\dynamDom.xml"
    Set root = Nothing
    Set dom = Nothing
    Exit Sub
    
ErrorHandler:
    MsgBox Err.Description
End Sub

To add dynamDOM.frm to the project

  • Copy the code listing above. Paste it into the Visual Basic code editor as the form_load subroutine, replacing any code fragments that are already there.

Next, run the project. The result should be the output shown in the following topic.