Jump to content

Changing XML Value


bbarrick

Recommended Posts

Could use a little help with this if you guys don't mind. This is just for testing purposes for a potential project. Trying to keep it simple at this point. I have an HTML file called projectform.html. Basic html input box with a button. I want to type a name into that box, click on the button and change the value of an XML file. projectform.html

<html><head>	<title>Information Screen Form</title>	<script src="jquery-1.9.1.min.js"></script>	<script src="project.js"></script>	<script>if (window.XMLHttpRequest)	{// code for IE7+, Firefox, Chrome, Opera, Safari	xmlhttp=new XMLHttpRequest();}		  	else	{// code for IE6, IE5	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");	}	xmlhttp.open("GET","datatest.xml",false);	xmlhttp.send();	xmlDoc=xmlhttp.responseXML;</script>  </head><body>	<div id="main">	<input type="text" id="inputname">	<input type="button" onclick="savename()"; value="Submit"></div></body></html>

project.jsfunction savename()

{	xmlDoc=loadXMLDoc("datatest.xml");	x=xmlDoc.getElementsByTagName("fname")[0].childNodes[0];	x.nodeValue=document.getElementById("inputname");}

XML just has the tags at this point. <?xml version="1.0" encoding="UTF-8" ?>

<
nurse
>
<
fname
>
Tonya
</
fname
>
</
nurse
>
Edited by bbarrick
Link to comment
Share on other sites

Try document.getElementById("inputname") .value Note. There should be no reason to use loadXMLDoc since you already loaded that doc in your XMLHttpRequest object.
Ok, thanks...I thought I had it in there. I commented out loadXMLDoc and added .value but it still didn't update the XML file.
Link to comment
Share on other sites

Javascript can't change a file, you'll need to send the data to a server-side script like PHP that can modify files.
That's what I've heard, but according to the tutorials on this forum you can update xml values with javascript. This isn't going to be an external site that I'm working on, so I don't want to go so far as to setting up a webserver and php. Basically I have a computer that is running a 24 hour display of information(right now it's powerpoint presentation). It's on the network, and I was hoping that I could create a series of automatically redirecting web pages to replaces the powerpoint presentation and an html form that would update an xml file and change what was being displayed on the information screen.
Link to comment
Share on other sites

When you change node values, you change the document object, which is not a real document. To change an actual document, you would need to stringify the xml and post it to your server; a script there would save it as a file. That's actually quite easy. But you'd also want to add things like security and validation.

Link to comment
Share on other sites

In other words, you're using Javascript to read data from an XML file into a local data structure in memory. You can change that data structure all you want, but it's not connected to the file in any way. Changing the data structure does not change the file. If you want to change the file then you can gather all of the XML data in the structure, send it to something like PHP, and have PHP update the actual file.

Link to comment
Share on other sites

When you change node values, you change the document object, which is not a real document. To change an actual document, you would need to stringify the xml and post it to your server; a script there would save it as a file. That's actually quite easy. But you'd also want to add things like security and validation.
Oh, so the document object is tossed out the same way a variable is in JavaScript once it's done with it?
Link to comment
Share on other sites

In other words, you're using Javascript to read data from an XML file into a local data structure in memory. You can change that data structure all you want, but it's not connected to the file in any way. Changing the data structure does not change the file. If you want to change the file then you can gather all of the XML data in the structure, send it to something like PHP, and have PHP update the actual file.
Thanks, that makes sense.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...