简体   繁体   中英

How to store data in xml file using javascript?

I am new to javascript and using it my project.In that I need to read xml file and then after manipulating that I want to store the updated values back in xml file.I am getting the values from xml file successfully but not able to store the values back to xml file. Here is the code I have tried.

        <html>
    <head>
        <title>
            Hello
        </title>
    </head>
<body>
<script>
function loadXML()
{
  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","data.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
//saving XML from document input fields
xmlDoc.getElementsByTagName("Name")[0].childNodes[0].nodeValue = document.getElementById("name").value;
xmlDoc.getElementsByTagName("Address")[0].childNodes[0].nodeValue = document.getElementById("address").value;
xmlDoc.getElementsByTagName("Contact")[0].childNodes[0].nodeValue = document.getElementById("contact").value;
xmlDoc.save();
}

</script>
<form action="Display.html" method="post">
<table>
    <tr>
        <td>Name :</td>
        <td>
            <input type="text" id="name"/>
        </td>
    </tr>
    <tr>
        <td>Address :</td>
        <td>
            <input type="text" id="address"/>
        </td>
    </tr>
    <tr>
        <td>Contact :</td>
        <td>
            <input type="text" id="contact"/>
        </td>
    </tr>
    <tr><td></td><td></td><td><input type="button" value="Submit" onclick="loadXML()"></td></tr>
</table>
</form>
</body>
</html>

Please help if anyone knows the answer.Please explain with example if possible Thanks in advance...

jQuery: you just use $.ajax() and send your data to something like save_my_xml.php via POST . There are some classes to work with XML in PHP, but if you get on well with Smarty I advise you to fetch your xml_template.tpl and then file_put_contents .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM