You are currently offline, waiting for your internet to reconnect

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

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.DOMDocument30Dim doc2 As MSXML2.DOMDocument30Dim doc2Node As MSXML2.IXMLDOMNodeSet doc1 = New MSXML2.DOMDocument30Set doc2 = New MSXML2.DOMDocument30doc1.Load "d:\xmlbooks1.xml"doc2.Load "d:\xmlbooks2.xml"For Each doc2Node In doc2.documentElement.childNodes  doc1.documentElement.appendChild doc2NodeNextMsgBox doc1.xmldoc1.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>					
IE
Properties

Article ID: 286817 - Last Review: 07/01/2004 20:52:06 - Revision: 3.2

  • Microsoft XML Parser 2.0
  • Microsoft XML Parser 2.5
  • Microsoft XML Parser 2.6
  • Microsoft XML Parser 3.0
  • Microsoft XML Core Services 4.0
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 3
  • Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 4
  • Microsoft Visual Basic 6.0 Professional Edition
  • kbhowto KB286817
Feedback