How To Merge Data in Two XML Documents by Programming the Document Object Model (DOM) (286817)



The information in this article applies to:

  • Microsoft XML 2.0
  • Microsoft XML 2.5
  • Microsoft XML 2.6
  • Microsoft XML 3.0
  • Microsoft XML 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0 SP3
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0 SP4
  • Microsoft Visual Basic Professional Edition for Windows 6.0

This article was previously published under Q286817

SUMMARY

This article documents a Visual Basic code sample that demonstrates how to program the MSXML Document Object Model (DOM) to merge the data that is contained in two identical XML documents.

MORE INFORMATION

Merging data that is contained in two or more identical XML documents into a single XML document is a common requirement. The code and method that is demonstrated in this article can be further expanded to merge three or more XML documents. If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:

305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds

  1. Open an empty text file in Microsoft Notepad.
  2. Copy and paste the following XML code into Notepad, and then save the file as xmlbooks1.xml:
    <?xml version="1.0"?>
    <Books>
       <Book>
          <Title>XML Step By Step</Title>
          <Publisher>MS Press</Publisher>
       </Book>
       <Book>
          <Title>Developing XML Solutions</Title>
          <Publisher>MS Press</Publisher>
       </Book>
    </Books>
    					
  3. Open a New text file in Notepad.
  4. Copy and paste the following XML code into Notepad, and then save the file as xmlbooks2.xml:
    <?xml version="1.0"?>
    <Books>
       <Book>
          <Title>Beginning XML</Title>
          <Publisher>Wrox</Publisher>
       </Book>
       <Book>
          <Title>Professional XML</Title>
          <Publisher>Wrox</Publisher>
       </Book>
    </Books>
    					
  5. Open a new Standard EXE project in Visual Basic 6.0. Form1 is created by default.
  6. On the Project menu, set a project reference to Microsoft XML, v3.0 or later.
  7. Place a Command Button onto Form1. Command1 is created by default.
  8. Copy and paste the following code into the Click event procedure of the CommandButton:
    ' In the ProgIDs below, change 30 to reflect the installed version of the Microsoft XML Parser.
    ' For example, use Dim doc1 As MSXML2.DOMDocument40 for MSXML 4.
    
    Dim doc1 As MSXML2.DOMDocument30
    Dim doc2 As MSXML2.DOMDocument30
    Dim doc2Node As MSXML2.IXMLDOMNode
    
    Set doc1 = New MSXML2.DOMDocument30
    Set doc2 = New MSXML2.DOMDocument30
    
    doc1.Load "d:\xmlbooks1.xml"
    doc2.Load "d:\xmlbooks2.xml"
    
    For Each doc2Node In doc2.documentElement.childNodes
      doc1.documentElement.appendChild doc2Node
    Next
    
    MsgBox doc1.xml
    
    doc1.save "d:\AllXMLBooks.xml"
    					
  9. The preceding code loads the two XML documents that are created in steps 1 through 4 into two MSXML DOMDocument objects. The code then merges the data in xmlbooks2.xml into the instance of the DOMDocument object containing the data in xmlbooks1.xml. This is achieved by looping through the child nodes of the document element of the doc2 DOMDocument object, and appending each of them to the document element of the doc1 DOMDocument. Finally, the merged XML is displayed in a Message box and persisted to a file on disk.
  10. Open AllXMLBooks.xml in Microsoft Internet Explorer to view the merged XML document, and note the following XML:
    <?xml version="1.0"?>
    <Books>
    	<Book>
    		<Title>XML Step By Step</Title>
    		<Publisher>MS Press</Publisher>
    	</Book>
    	<Book>
    		<Title>Developing XML Solutions</Title>
    		<Publisher>MS Press</Publisher>
    	</Book>
    	<Book>
    		<Title>Beginning XML</Title>
    		<Publisher>Wrox</Publisher>
    	</Book>
    	<Book>
    		<Title>Professional XML</Title>
    		<Publisher>Wrox</Publisher>
    	</Book>
    </Books>
    					

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto KB286817