Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

Visual Basic Code (importNode.frm)

Private Sub Form_Load()

Dim domFree As FreeThreadedDOMDocument50
Dim domApt As DOMDocument50
Dim node As IXMLDOMNode
Dim clone As IXMLDOMNode
Dim msg As String

msg = ""
Set domFree = New FreeThreadedDOMDocument50
Set domApt = New DOMDocument50

domApt.async = False
If False = domApt.Load(App.Path + "\doc1.xml") Then
    MsgBox "can't load doc1.xml"
    Exit Sub
End If

domFree.async = False
If False = domFree.Load(App.Path + "\doc2.xml") Then
    MsgBox "can't load doc2.xml"
    Exit Sub
End If

' Copy a node from domFree to domApt:
'   Fetch the "/doc" (node) from domFree (doc2.xml).
'   Clone node for import to domApt.
'   Append clone to domApt (doc1.xml).
'
Set node = domFree.selectSingleNode("/doc")
Set clone = domApt.importNode(node, True)
domApt.documentElement.appendChild clone
domApt.documentElement.appendChild domApt.createTextNode(vbNewLine)
msg = msg + "doc1.xml after importing /doc from doc2.xml:"
msg = msg + vbNewLine + domApt.xml + vbNewLine
Set node = Nothing
Set clone = Nothing

' Clone a node using importNode() and append it to self:
'   Fetch the "doc/b" (node) from domApt (doc1.xml).
'   Clone node using importNode() on domApt.
'   Append clone to domApt (doc1.xml).
'
Set node = domApt.selectSingleNode("/doc/b")
Set clone = domApt.importNode(node, True)
domApt.documentElement.appendChild domApt.createTextNode(vbTab)
domApt.documentElement.appendChild clone
msg = msg + "doc1.xml after import /doc/b from self:"
msg = msg + vbNewLine + domApt.xml + vbNewLine
Set node = Nothing
Set clone = Nothing

' Clone a node and append it to the dom using cloneNode():
'   Fetch "doc/a" (node) from domApt (doc1.xml).
'   Clone node using cloneNode on domApt.
'   Append clone to domApt (doc1.xml).
'
Set node = domApt.selectSingleNode("/doc/a")
Set clone = node.cloneNode(True)
domApt.documentElement.appendChild clone
msg = msg + "doc1.xml after cloning /doc/a from self:"
msg = msg + vbNewLine + domApt.xml + vbNewLine
Set node = Nothing
Set clone = Nothing

domApt.save App.Path + "\out.xml"
msg = msg + "a new document was saved to out.xml in the current working directory."

MsgBox msg
End Sub

Try It!

  1. Copy the first XML data (doc1.xml), and paste it into a text file. Save the file as doc1.xml.
  2. Copy the second XML listing (doc2.xml), and paste it into a text file. Save the file as doc2.xml, in the same directory where you saved doc1.xml.
  3. Create a Standard EXE project in Visual Basic. Save the empty project as importNode.vbp to the same directory where you saved doc1.xml and doc2.xml. Name the form file importNode.frm.
  4. Create a reference to MSXML 5.0. To do this, select References... from the Project menu, then check the box for Microsoft XML, v50.
  5. Copy the Visual Basic code listing above, and paste it into the form_load subroutine.
  6. Execute the code by selecting Start from the Run menu.
  7. Verify that the output is the same as that listed in Output for the importNode Example.