Jump to content

Adding Elements And Attributes Using The Dom


Bonzo

Recommended Posts

Ok, I have an XML file, MarkerList.xml in the root directory, I'm trying to make it so that when the user presses a button on a webpage a function is executed to add an element to the file and add some attributes to that element. I have been using the XML DOM tutorial as my reference, plus various other sources when it didn't work, but I can't seem to fathom the problem. It's probably something really simple I've missed or don't quite understand, but its driving me crazy!MarkerList.xml:

<?xml version="1.0" encoding="iso-8859-1"?><markers>  <marker lat="53.407076" lng="-2.973175"  />  <marker lat="53.45494" lng="-2.735252"  /></markers>

And my function as it stands now:

function addXML(){        var xmlDoc;        try{        //load XML for IE        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");        }        catch(e){        //load XML for firefox        xmlDoc = document.implementation.createDocument("", "", null);        }        xmlDoc.async="false";        xmlDoc.load("MarkerList.xml");        var x;        var marker = xmlDoc.createElement("marker")[0];          x = xmlDoc.getElementsByTagName("markers");          x[0].setAttribute("lat", "53.451669");          x[0].setAttribute("lon", "-2.887045");          x.appendChild(marker);          xmlDoc.save("MarkerList.xml");     }

In firefox I get in the error console "x[0] is undefined" and in IE I get "Object doesn't support this property or method" and references the line for "x.appendChild(marker);". Any input would be greatly appreciated.

Link to comment
Share on other sites

You have some things wrong:This doesn't work because it has a [0] at the end

var marker = xmlDoc.createElement("marker")[0];

I'm under the impression that you actually want to add these attributes to the <marker> element, not the <markers> element:

x = xmlDoc.getElementsByTagName("markers");x[0].setAttribute("lat", "53.451669");x[0].setAttribute("lon", "-2.887045");

Javascript can't save files, you would need PHP for that:

xmlDoc.save("MarkerList.xml");

I don't know why the browsers are giving you those errors, but this code is better organized and possibly solves some of the mistakes you made:

var doc = xmlDoc.documentElement;var marker = doc.createElement("marker");marker.setAttribute("lat", "53.451669");marker.setAttribute("lon", "-2.887045");doc.getElementsByTagName("markers")[0].appendChild(marker);

The XML object has been updated, however, you can't save it in any way

Link to comment
Share on other sites

Thank you for the reply. I altered my code as you suggested, unfortunately Firefox still insists that (in your example) doc = null. Which doesn't entirely make sense to me because it seems logically sound.Also, JS/AJAX provide no mechanism for saving XML files at all? Odd.*edit*Perhaps I didn't make my intentions clear, I'm not wanting to save the XML file to the client's machine, but rather for it to take arguements from the web page (input by the client) and update an XML file stored on the server.

Link to comment
Share on other sites

Javascript is not able to save files neither on the client nor on the server. You'd have better luck sending the data to a PHP page which will put the information into the XML file instead.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...