Jump to content

Editing an xml file with javascript


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 post
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 : "";}

Edited by Deirdre's Dad
Link to post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...