如何使用javascript在xml文件中存储数据?

如何使用javascript在xml文件中存储数据?,javascript,dom,Javascript,Dom,我不熟悉javascript并将其用于我的项目。因为我需要读取xml文件,然后在操作之后,我希望将更新后的值存储回xml文件。我正在成功地从xml文件获取值,但无法将值存储回xml文件。 这是我尝试过的代码 <html> <head> <title> Hello </title> </head> <body> <script>

我不熟悉javascript并将其用于我的项目。因为我需要读取xml文件,然后在操作之后,我希望将更新后的值存储回xml文件。我正在成功地从xml文件获取值,但无法将值存储回xml文件。 这是我尝试过的代码

        <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>

你好
函数loadXML()
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“data.xml”,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
//从文档输入字段保存XML
xmlDoc.getElementsByTagName(“名称”)[0]。childNodes[0]。nodeValue=document.getElementById(“名称”).value;
xmlDoc.getElementsByTagName(“地址”)[0]。childNodes[0]。nodeValue=document.getElementById(“地址”).value;
xmlDoc.getElementsByTagName(“联系人”)[0]。childNodes[0]。nodeValue=document.getElementById(“联系人”).value;
xmlDoc.save();
}
姓名:
地址:
联系人:
如果有人知道答案,请提供帮助。如果可能,请举例说明
提前感谢…

jQuery:您只需使用
$.ajax()
并通过
POST
将数据发送到
save\u my\u xml.php
之类的东西。PHP中有一些类可以使用XML,但如果您与Smarty相处得很好,我建议您
获取
您的
XML\u模板.tpl
,然后
文件\u放置\u内容

jQuery:您只需使用
$.ajax()
并通过
POST
将数据发送到
保存我的XML.PHP
之类的地方。PHP中有一些类可以使用XML,但是如果您与Smarty相处得很好,我建议您
获取
您的
XML\u模板.tpl
,然后
文件\u放置\u内容

您将需要一些服务器端代码来为您保存。欢迎使用StackOverflow!我已经为您修复了问题中的代码格式。请花一点时间阅读提问区右侧以及问题区正上方[?]的“如何设置格式”框。(在“提问”框下还显示了一个预览框,您可以在其中预览您的问题。)您需要一些服务器端代码来为您保存。欢迎使用StackOverflow!我已经为您修复了问题中的代码格式。请花一点时间阅读提问区右侧以及问题区正上方[?]的“如何设置格式”框。(提问框下方还显示了一个预览框,您可以预览您的问题。)感谢回复,但我只能使用纯javascript完成任务。感谢回复,但我只能使用纯javascript完成任务。