Link to home
Create AccountLog in
Avatar of MatthewNicoll

asked on

MSXML: how to copy an entire document

Using MSXML, how do I make a copy of an entire XML document?

Instead of loading an xml file twice, into DOMdocument A and B, I want to load it once, into A, then copy it into B.  I want B to be EXACTLY as if I had loaded it from the xml file.

I tried getting the root element of document A, then using appendChild to insert that into B, but that did not copy over the comments which were above the root element.

(An example in VB would be nice!)
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

What kind of VB? VB6, VBA, .Net?
Avatar of MatthewNicoll


I am actually coding in Fortran 90.
The question is MSXML-dependent, not language dependent.

I mentioned VB because it is the most common language which I readily understand.

(Forgive me if I have posted this question in the wrong place.  I confess I have a hell of a time figuring out where to post questions on Experts Exchange!)
Avatar of zc2
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Yes, thanks, the cloneNode method of the document object did the job.  
I had seen the cloneNode method, but I had not considered the DOM document itself to be a node.
I also found the following note in the MSXML documentation for the cloneNode method:
"If the node is the DOMDocument node, it is safer to clone the document using the save method"

Apparently the document Save method can be used to save to another document ,if the destination provided is a document object instead of a file name.
I didn't have any luck with that, but because the cloneNode method works, I've stopped experimenting.
You're absolutely right, MSXML allows to load one XML DOM document from another.
Set doc1 = CreateObject("MSXML2.DOMDocument")
Set doc2 = CreateObject("MSXML2.DOMDocument")

Open in new window