0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 by y=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
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.
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.
Updated answer with relevant information that should help you hopefully.
0

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.