Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating / Deleting nodes using javascript

Status
Not open for further replies.

simonchristieis

Programmer
Jan 10, 2002
1,144
GB
I have an xml file, I can access the file and display, what I want to do is change the data in the file.
eg


var xmlDoc = new ActiveXObject("msxml2.DOMDocument.4.0");

xmlDoc.async=false;xmlDoc.resolveExternals=false;
xmlDoc.validateOnParse=false;

xmlDoc.load("xml/test.xml");

tName = "Mr Wallis"

nodes=xmlDoc.selectNodes("/school/teachersname='" +tName+ "']/*")


but what I would like to do is change the name to "Mr Williams"

then I would like to delete the entire node set

Can anyone help or point me towards a tutorial ?

Thanks in advance

 
>> Can anyone help or point me towards a tutorial ?

Are you using the MSDN MSXML Reference? You can't do much without that:
The following code works on this XML:
Code:
<root>
   <tnode id=&quot;1&quot; action='delete'/>
   <tnode id=&quot;2&quot; action='update'/>
   <tnode id=&quot;3&quot;>Hell World</tnode>
   <tnode id=&quot;4&quot; />
</root>
Script code
Code:
var xmlDoc = new ActiveXObject(&quot;msxml2.DOMDocument.4.0&quot;);

xmlDoc.async=false;xmlDoc.resolveExternals=false;
xmlDoc.validateOnParse=false;

xmlDoc.load(&quot;C:\\Research\\test.xml&quot;);

nodes=xmlDoc.selectNodes(&quot;//tnode[@id=3]/text()&quot;);

var oNode = nodes.item(0);
oNode.nodeValue = &quot;Goodbye World&quot;;
xmlDoc.save(&quot;C:\\Research\\testsave.xml&quot;);

-pete
 
Thank you for your help.

I have tried the code in the page and get a permission denied error.

Do you have any ideas ?

Thanks in advance

Simon
 
>> Do you have any ideas ?

Sure. My idea is that has nothing to do with XML. Security settings are part of the OS and as a programmer you should know how to gain write access to files on hard drives. Since you have not provided any information about your environment that is all the ideas I am going to have.

-pete
 
Thanks for your time again pete.

After checking all the file permissions, and trawling through the internet, all I have managed to find is that it seems to be an issue with browser security, I have changed the file extension to .hta and it works fine,
did you manage to make your code work in an .htm file ?

simon
 
>> did you manage to make your code work in an .htm file ?

Ummm… your original post made no mention of executing in a browser context of any sort. You need to post good questions with complete technical information to get good/on target answers. I developed my code as a .js file and executed it directly in the Windows Script Host and of course it worked fine.

Yes there are tight restrictions under which that code might be allowed to execute from within a browser. I don’t know them all. HTA files are one of them. For the most part local disk access from a browser context is not allowed for script code embedded in HTML.


-pete
 
Im sorry I hadn't made myself clearer. Thanks for your halp anyway - I will go ahead and use an .hta file , after all this is only for a game I'm developing, so I can decide platform / browser etc.

Thanks for your help anyway

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top