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 biv343 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 2 xml files using DOM??

Status
Not open for further replies.

cmckenna82

Technical User
Feb 1, 2004
1
GB
Hi,

I need to create the following application/tool,

I have to merge XML files (using DOM scripting and JavaScript). I will have approx 5-8 files (representing scientific abstracts) and will have to be able to merge any of the files with each other. I will have a basic interface (HTML page), this will allow the user to select one XML file from a drop down, select a different file from another drop down then click a merge button and the result will be outputted below.

In a nut shell I am really lost and don't know where to start. I have been doing a lot of research although i am finding it hard to start any kind of coding. All I can find on the net is examples using XSLT to merge XML files??

I do have some experience using DOM but simply to output certain parts of an xml file into a html table.

A worked example (including code) of merging two xml files using DOM, (doesn't matter how basic) would be very very helpful. Any advice or feedback would be greatly appreciated also as i am running out of time.

In summary if anyone can offer any type of guidance or advice I will be extremely greatly.

Regards
Ciaran

ps please feel free to email me with any suggestions/examples/advice or questions

 
Hi Ciaran,

Here is a VBScript example. Basically, you create 2 DOM objects, load the 2 files. Select the nodes from one document and add it to the other document. Make sure you add it to the right place in the XML tree.

Good luck.



Dim objDoc1, objDoc2, objNode

Set objDoc1 = CreateObject("MSXML2.DOMDocument.4.0")
objDoc1.async = False
Set objDoc2 = CreateObject("MSXML2.DOMDocument.4.0")
objDoc2.async = False

If Not objDoc1.Load("C:\file1.xml") Then
'Failed to load XML file.
'Exit script
End If

If Not objDoc2.Load("C:\file2.xml") Then
'Failed to load XML file.
'Exit script
End If

'Add nodes from file 1 to file 2
For Each objNode In objDoc1.selectNodes("/store/book")
objDoc2.documentElement.appendChild objNode.cloneNode(True)
Next

'Save file 2
objDoc2.save "C:\file2.xml"

Set objDoc1 = Nothing
Set objDoc2 = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top