Jump to content

XML Copy child node and append to parent


Recommended Posts

I am attempting to copy a child node and all its children below a parent node and append the copy to the same parent.

The method to select either of the two child node would then be "SelectSingleNode//Parent/Child [0]" for the first child [1] for the second child.

I know that node last and node fist would work here but I plan to create many child nodes once I learn this step.

Example

I am attempting to change this:

<template version="5">
    <genInfo>
        <GA>
            <GASM>
                <GASMT>Text</GASMT>
            </GASM>
            <GASM/>
        </GA>
    </genInfo>
</template>

Into this:

<template version="5">
    <genInfo>
        <GA>
            <GASM>
                <GASMT>Text</GASMT>
            </GASM>
            <GASM/>
        </GA>
    </genInfo>

 <genInfo>
        <GA>
            <GASM>
                <GASMT>Text</GASMT>
            </GASM>
            <GASM/>
        </GA>
    </genInfo>
</template>

The following code produces no error but does nothing:

Local $oXML = ObjCreate("Microsoft.XMLDOM")  ;For all XML fie operations

$oXML.load("C:\Utilities\Temp\CloneRPT.hr4")
$oNodeChild = $oXML.SelectSingleNode('//template/genInfo')
$oNodeParemt = $oXML.SelectSingleNode('//template')
$oNodeParemt.appendchild($oNodeChild)
$oXML.Save("C:\Utilities\Temp\CloneRPT.hr4")

Thanks

Link to comment
Share on other sites

Try this instead :

Local $oXML = ObjCreate("Microsoft.XMLDOM")  ;For all XML fie operations

$oXML.load("CloneRPT.xml")
$oNodeChild = $oXML.SelectSingleNode('//template/genInfo')
ConsoleWrite(IsObj($oNodeChild) & @CRLF)
$oNodeParent = $oXML.SelectSingleNode('//template')
ConsoleWrite(IsObj($oNodeParent) & @CRLF)
$oNewNode = $oNodeChild.cloneNode(True)
$oNodeParent.appendchild($oNewNode)
ConsoleWrite($oNodeParent.xml & @CRLF)

You need to create a new object if you want the appendChild to be successful...

Link to comment
Share on other sites

Nine,

The child node /genInfo got copied and added to //template, but none of its child nodes or its sibilings got copied / cloned.

I thought that the "True" statement was supposed to do this.

 

 

Edited by AutoitMike
Link to comment
Share on other sites

Link to comment
Share on other sites

That is kind of strange.  Based on MSDN :

Quote

Boolean. A flag that indicates whether to recursively clone all nodes that are descendants of this node. If True, creates a clone of the complete tree below this node. If False, clones this node and its attributes only.

Seem to be working, with and without quotes on me, even with simple number 0 and 1.  Wonder why it is not working for you (without).  What is your setup ?

Anyway, most important, you found a way to make it work...

Edited by Nine
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...