开发者

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>开发者_StackOverflow

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜