Jump to content

Editing an xml file with javascript


gsingh2011

Recommended Posts

Hi, I just learned XML and I have to say its very cool and very useful. I was wondering if there built in javascript functions to edit the text between the tags. Just like you can use xmlDoc.getElementsByTagName("tag")[0].childNodes[0].nodeValue to get the value of the text node, is there a function to set it?

Link to comment
Share on other sites

Use the = sign. Really.Advice. Use some utility functions for getting and setting those values. If the tag you're after has no text content, then it has no childnodes, and trying to access one will raise an exception. So you need to do all kinds of conditional junk every time you want to read something.Here are a couple of functions that you may use any way you like. (I had them in easy reach.)

function set_node_value (n, v) {	var tn;	if (n.childNodes[0]) {		n.childNodes[0].nodeValue = v;	} else {		tn = xmlDoc.createTextNode(v);		n.appendChild(tn);	}}function get_node_value (n) {	return n.childNodes[0] ? n.childNodes[0].nodeValue : "";}

Link to comment
Share on other sites

Use the = sign. Really.Advice. Use some utility functions for getting and setting those values. If the tag you're after has no text content, then it has no childnodes, and trying to access one will raise an exception. So you need to do all kinds of conditional junk every time you want to read something.Here are a couple of functions that you may use any way you like. (I had them in easy reach.)
function set_node_value (n, v) {	var tn;	if (n.childNodes[0]) {		n.childNodes[0].nodeValue = v;	} else {		tn = xmlDoc.createTextNode(v);		n.appendChild(tn);	}}function get_node_value (n) {	return n.childNodes[0] ? n.childNodes[0].nodeValue : "";}

Well I tried this out,
xmlDoc.getElementsByTagName("days")[0].childNodes[0].nodeValue = document.getElementsByName("days1")[0].value;			alert(xmlDoc.getElementsByTagName("days")[0].childNodes[0].nodeValue);

And it output the correct value, but it didn't write the change to the xml file. That's what I need to do, have a permanent change to the file.

Link to comment
Share on other sites

Right. All you've done is made changes to a document object that exists in RAM. What you need to do is convert the document object to a string (i.e., raw data) and send the string back to your server. The server then writes the updated string to a file. It could be the original file or a new file.Here is a cross-browser function for converting your XML doc to a string:

function xml2string (d) {	var s;	if (window.XMLSerializer) {		s = new XMLSerializer();		return s.serializeToString(d);	} else {		return d.xml;	}}

The first part is standards compliant. The else clause picks up older versions of IE. Pass xmlDoc to the function, and it returns the stringified version. Send it to your server using the same techniques you'd use to send any string to your server.

Link to comment
Share on other sites

You could programmatically add the string to a hidden form input before posting. That would look something like this:<input type="hidden" name="variable" id="myID">and in your script:document.getElementById('myID').value = string;Or you could use AJAX and do essentially the same thing.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...