Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to merge two XML files?? 1

Status
Not open for further replies.

bchagnon

Programmer
Oct 3, 2002
4
0
0
CA
Hi!

I want to merge two xml files.

Ex: I want to merge the file 2 into the file 1.

file 1
<aaa>
<bbb>Canada</bbb>
<eee>Belgium</eee>
</aaa>

file 2
<aaa>
<bbb>USA</bbb>
<ccc>Canada</ccc>
<ddd>France</ddd>
</aaa>

result
<aaa>
<bbb>USA</bbb>
<ccc>Canada</ccc>
<ddd>France</ddd>
<eee>Belgium</eee>
</aaa>

Do you have a good code sample to do that?

P.S. The merge fonction in the Dataset object is not a good way for me ;)
 
This code in VBs copies all the nodes from on file to the other one :
Code:
dim objXML, objXML2, objRootNode, objRootNode2, tmpNode

  Set objXML = CreateObject(&quot;Microsoft.XMLDOM&quot;)
  objXML.async = False
  objXML.load (&quot;c:\File1.xml&quot;)

  Set objXML2 = CreateObject(&quot;Microsoft.XMLDOM&quot;)
  objXML2.async = False
  objXML2.load (&quot;c:\File1.xml&quot;)

  Set objRootNode = objXML.SelectSingleNode[&quot;./aaa&quot;]
  Set objRootNode2 = objXML2.SelectSingleNode[&quot;./aaa&quot;]

  for each tmpNode in objRootNode2.childNodes
	objRootNode.appendChild(tmpNode)
  next
Note that it doesnt verify if the node allready exist before copying it. Water is not bad as long as it stays out human body ;-)
 
This code in VBs copies all the nodes from on file to the other one :
Code:
dim objXML, objXML2, objRootNode, objRootNode2, tmpNode

  Set objXML = CreateObject(&quot;Microsoft.XMLDOM&quot;)
  objXML.async = False
  objXML.load (&quot;c:\File1.xml&quot;)

  Set objXML2 = CreateObject(&quot;Microsoft.XMLDOM&quot;)
  objXML2.async = False
  objXML2.load (&quot;c:\File2.xml&quot;)

  Set objRootNode = objXML.SelectSingleNode[&quot;./aaa&quot;]
  Set objRootNode2 = objXML2.SelectSingleNode[&quot;./aaa&quot;]

  for each tmpNode in objRootNode2.childNodes
	objRootNode.appendChild(tmpNode)
  next
Note that it doesnt verify if the node allready exist before copying it. Water is not bad as long as it stays out human body ;-)
 
Thanks for your answers guys, but something is missing.

I have to merge 2 files, but if the element in the file #1 is in the file #2, i want to replace it in my master file. If this element is not in the file #1, I have to add it.

In my example, you see that all elements in file 1 are there, but if this element is in the file #2 too, we have to replace it. If is not in the file, we have to append the element in the file #1.

I think I have to loop into all elements and check in the file #1 to know if the element is there, and do the correct job (replace or append) after that.

If you have some idea, that's would be very nice!

Benoît Chagnon
Developper
Posera Software

 
To check if the node allready exists, do that :
Code:
  for each tmpNode in objRootNode2.childNodes
    id = tmpNode.NodeName
    XSLQuery = &quot;.//&quot; & NodeName
    Set TmpNode2 = objRootNode.SelectSingleNode(XSLQuery)
    if IsNothingNode then
       objRootNode.appendChild(tmpNode)
    end if
  next

function IsNothingNode(Node2Test)
dim junk
  on error resume next 
  junk = Node2Test.NodeName
  if err.number <> 0
     err.clear
     IsNothingNode = true
  else
     IsNothingNode = false
  end if
end function
Water is not bad as long as it stays out human body ;-)
 
In VB.Net you can do this:

Imports System.Xml
Module XMLMerge

Sub MergeXML(ByVal XMLPath As String, ByVal XMLFile1 As String, ByVal XMLFile2 As String, ByVal XMLFile3 As String)

Dim xmlreader1 As New XmlTextReader(XMLPath & XMLFile1)
Dim xmlreader2 As New XmlTextReader(XMLPath & XMLFile2)
Dim xmlreader3 As New XmlTextReader(XMLPath & XMLFile3)

Dim ds1 As New DataSet()
Try
ds1.ReadXml(xmlreader1)

Dim ds2 As New DataSet()
ds2.ReadXml(xmlreader2)

Dim ds3 As New DataSet()
ds3.ReadXml(xmlreader3)

ds1.Merge(ds2)
ds1.Merge(ds3)
ds1.WriteXml(XMLPath & &quot;FinalFile.XML&quot;, XmlWriteMode.IgnoreSchema)
Console.WriteLine(&quot;Completed merging XML documents&quot;)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.Read()

End Sub

End Module


Hope this helps.

Persistence....Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination alone are omnipotent. -Calvin Coolidge
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top