Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 286817 - Last Review: July 1, 2004 - Revision: 3.2
How To Merge Data in Two XML Documents by Programming the Document Object Model (DOM)
This article was previously published under Q286817
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.
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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=305019/EN-US/
)
INFO: MSXML 4.0 Specific GUIDs and ProgIds
- Open an empty text file in Microsoft Notepad.
- 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>
- Open a New text file in Notepad.
- 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>
- Open a new Standard EXE project in Visual Basic 6.0. Form1 is created by default.
- On the Project menu, set a project reference to Microsoft XML, v3.0 or later.
- Place a Command Button onto Form1. Command1 is created by default.
- 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"
- 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.
- 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>
APPLIES TO
- 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
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate