tags:

views:

161

answers:

2

Hi, Im trying to delete a node in XML file using Javascript. The used browser is Firefox. Im reaching the node which I want to delete successfuly, then Im trying delNode.ParentNode.RemoveChild(delNode) but when i went back to the xml file on my harddisk, the node was still there. I need to delete the whole node (not only children). If someone could guide me to what to do, I'll be so greatful ;) Thanks

+1  A: 

That should remove delNode. Are you getting an error? What is the result of delNode.parentNode.removeChild(delNode) ? I do the same thing all the time and it works for me.

I'm sorry I missed the part about writing it back to the file. Use nsiFile to do that and a chrome:// url. In order to take advantage of these things you'll probably have to create a directory with a directory called 'chrome' which will have your script, a chrome.manifest file.

apphacker
Thanks for answering. when implementing this method delNode.parentNode.removeChild(delNode), my code stop on this line and doesn't do the rest of the functions. I tried another way byy=xmlDoc.getElementsByTagName("file")[0];window.alert(y);xmlDoc.documentElement.removeChild(y);In the latter case, i didn't get an error and all the code was implemented in my browser, but when i went back to the xml file on my harddisk, the node was still there. I don't know whats wrong since it is the first time i try this.Any idea! and thanks a lot
Beso
Erm. Altering the DOM only changes your in-memory loaded copy, it doesn't write back to the file you loaded it from. You would need to re-serialise the DOM back to an XML file to replace the disc copy. If you are in a web page (not browser chrome) you will have no access to write to the local filesystem at all.
bobince
Oh I missed that whole part about writing it back to file. That's right you need to use the chrome:// url and nsiFile class to write the file to disk.
apphacker
Updated answer with relevant information that should help you hopefully.
apphacker
A: 

Hi, Thanks for the help. I couldn't find a way to change the content of the file in javascript so I used PHP. I was trying simply to delete the first node in my xml activity1.xml ... here is the code

$xdoc = new DomDocument; $xdoc->Load('images\activity\activity1.xml'); $xdoc->documentElement->removeChild($xdoc->documentElement->childNodes->item(1)); $xdoc->save('images\activity\activity1.xml');

it simply delete the first node whatever children it has ;)

Beso