如何使用javascript将数据存储在xml文件中?
我是 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>
如果有人知道答案,请帮忙。如果可能,请举例说明 提前致谢...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery:您只需使用
$.ajax()
并通过POST
将数据发送到save_my_xml.php
等。有一些类可以在 PHP 中使用 XML,但如果您能很好地使用 Smarty,我建议您获取
您的xml_template.tpl
,然后file_put_contents
代码>.jQuery: you just use
$.ajax()
and send your data to something likesave_my_xml.php
viaPOST
. There are some classes to work with XML in PHP, but if you get on well with Smarty I advise you tofetch
yourxml_template.tpl
and thenfile_put_contents
.