Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: MSXML replaceChild syntax for replacing nodes?

From: "Joe Fawcett" <joefawcett@---------.------>
To: NULL
Date: 7/11/2008 6:55:00 PM



<olaf.boldt@g...> wrote in message 
news:463a1ab3-28a0-4f3b-baaf-9bcd8a791287@z......
> I have two xml files and I want to use VBA to replace a specific node
> in one of the xml files with a specific node in the other file. Both
> files have exactly the same structure. Only the root is different.
>
> The first node is:
> xmlDoc.ChildNodes(1).ChildNodes(1).ChildNodes(1)
> And the second node is:
> xmlDoc2.ChildNodes(1).ChildNodes(1).ChildNodes(1)
>
> What I want is to replace the second node in xmlDoc2 with the first
> one in xmlDoc.
>
> However, when I use the following code:
>
> xmlDoc2.replaceChild xmlDoc.ChildNodes(1).ChildNodes(1).ChildNodes(1),
> xmlDoc2.ChildNodes(1).ChildNodes(1).ChildNodes(1)
>
> I get the error message: "Insert position Node must be a Child of the
> Node to insert under."
>
> What's the correct syntax?
>
It's replace child so node to call the method on must be the parent of the 
old node, not a higher level ancestor.
You should also use importNode (or cloneNode if using MSXML before version 
5.0) as you shouldn't mix nodes from different documents.
importNode basically copies a node and re-assigns the owner Document to the 
new document.
Set clone = 
xmlDoc2.importNode(xmlDoc.ChildNodes(1).ChildNodes(1).ChildNodes(1), True)
Set newNodeReference = 
xmlDoc2.ChildNodes(1).ChildNodes(1).ReplaceChild(clone, 
xmlDoc2.ChildNodes(1).ChildNodes(1).ChildNodes(1))


-- 

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

 



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent