如何使用javascript将数据存储在xml文件中? [英] How to store data in xml file using javascript?

查看:151
本文介绍了如何使用javascript将数据存储在xml文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的javascript并使用它我的项目。在那我需要读取xml文件,然后在操作后,我想将更新的值存储在xml文件中。我从xml文件中获取值成功但不是能够将值存储回xml文件。
这是我试过的代码。

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:你只需使用 $。ajax()并通过 POST 将您的数据发送到类似 save_my_xml.php 的内容。有一些类可以在PHP中使用XML,但是如果您与Smarty一起使用,我建议您 fetch 您的 xml_template.tpl 然后 file_put_contents

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.

这篇关于如何使用javascript将数据存储在xml文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆