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
-----------------+
tom dyson
t: +44 (0)1608 811870
m: +44 (0)7958 752657
http://torchbox.com
> I used your code snippet like this:
>
> <cfobject action="create" class="MSXML2.DOMDocument" name="xmldoc">
> <cfset xml = "<mother><daughter>This is a test</daughter></mother>">
> <cfscript>
> xmldoc.loadXML('#xml#');
> root = xmldoc.documentElement;
> todelete=xmldoc.selectSingleNode("mother/daughter");
> deleteSuccess = root.removeChild(todelete);
> </cfscript>
>
> And it worked perfectly. But then I tried it like this:
>
> <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#');
> root = xmldoc.documentElement;
> todelete=xmldoc.selectSingleNode("mother/seconddaughter/baby");
> deleteSuccess = root.removeChild(todelete);
> </cfscript>
>
> And here I got the "Unknown Exception" error. Note that I'm deleting a node
> that is on the third level of the tree.
> Anybody got any ideas?
>
> Rafi
>
> ----Original Message-----
>
> Hi Rafael
>
> The removeChild method of an MSXML object certainly works in ColdFusion -
> here's an example of it in action in our XML content management system:
>
> <cfobject action="create" class="MSXML2.DOMDocument" name="xmldoc">
> <cfscript>
> xmldoc.loadXML('#form.xml#');
> root = xmldoc.documentElement;
> todelete=xmldoc.selectSingleNode("article_xml/object[@id='#form.id#']");
> deleteSuccess = root.removeChild(todelete);
> </cfscript>
>
> I'm confident that the insertbefore and replacechild methods will work too -
> the only known problems with ColdFusion and MSXML appear when using CF5 and
> the XSLStylesheet interface. Unfortunately MSXML doesn't return any useful
> error messages to CFOBJECT (unlike other COM objects) so it's very difficult
> to know what's going wrong.
>
> If the little code snippet above doesn't help, perhaps you could show us
> your code?
>
> Tom
-----------------------+
cf-xml mailing list
list: [EMAIL PROTECTED]
admin: [EMAIL PROTECTED]
home: http://torchbox.com/xml