Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Fri, 11 Jul 2008 10:23:12 -0700 (PDT),    group: microsoft.public.xml        back       


MSXML replaceChild syntax for replacing nodes?   
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?
date: Fri, 11 Jul 2008 10:23:12 -0700 (PDT)   author:   unknown

Re: MSXML replaceChild syntax for replacing nodes?   
wrote in message 
news:463a1ab3-28a0-4f3b-baaf-9bcd8a791287@z66g2000hsc.googlegroups.com...
> 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
date: Fri, 11 Jul 2008 18:55:23 +0100   author:   Joe Fawcett am

Re: MSXML replaceChild syntax for replacing nodes?   
On Jul 11, 10:55 am, "Joe Fawcett" <joefawc...@newsgroup.nospam>
wrote:
>  wrote in message
>
> news:463a1ab3-28a0-4f3b-baaf-9bcd8a791287@z66g2000hsc.googlegroups.com...
>
> > 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

Thanks for the quick answer. I'll give it a try.
date: Fri, 11 Jul 2008 11:18:11 -0700 (PDT)   author:   unknown

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us