Tom, thanks again for the response. Just noticed while looking at the CF_XMLDelete
tag's code that it has the same mistake I was making:
22: <!--- Select the node to be deleted --->
23: <CFSET deleteNode=docElement.selectSingleNode("#Attributes.XPath#")>
24:
25: <!--- Delete the node --->
26: <CFSET temp = docElement.removeChild(deleteNode)>
Which would work for child nodes but not deeper nodes. This could be fixed by adding
the additional step you provided.
Rafi
---Original Message---
Hi Rafi,
Your code fails because the 'baby' element is not a child of the root
element (it's a grandchild). The removeChild method must be used with a
direct ancestor of the element to be deleted: you can access this ancestor
using the parentNode property. Here's the code you need:
<cfobject action="create" class="MSXML2.DOMDocument" name="xmldoc">
<cfset xml = "<mother><firstdaughter>This is a test</firstdaughter>
<seconddaughter><baby>Test</baby></seconddaughter></mother>">
<cfscript>
xmldoc.loadXML('#xml#');
// specify the condemned element...
todelete=xmldoc.selectSingleNode("mother/seconddaughter/baby");
// find its parent
todeleteparent = todelete.parentNode;
// remove the parent's child
deleteSuccess = todeleteparent.removeChild(todelete);
// prove it worked
writeoutput('before: ' & htmleditformat(xml));
writeoutput('<hr />after: ' & htmleditformat(xmldoc.xml));
</cfscript>
Tom
-----------------------+
cf-xml mailing list
list: [EMAIL PROTECTED]
admin: [EMAIL PROTECTED]
home: http://torchbox.com/xml