2011-07-02 51 views
1

我是新来的JavaScript和使用它我的项目。在那我需要读取XML文件,然后操纵后,我想将更新的值存储回xml文件。我从XML文件成功,但无法获得值将值存储回xml文件。 这是我试过的代码。如何使用JavaScript将数据存储在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> 

如果有人知道answer.Please用例子来解释如果可能的话 在此先感谢...

+3

您需要一些服务器端代码来为您节约。 – alex

+3

欢迎来到StackOverflow!我已经为您解决了问题中的代码格式。请花点时间阅读“问题提示”区域右侧的方便的**如何格式化**框和**中的[链接页面](http://stackoverflow.com/editing-help) ?] **就在问题区域上方。 (还有一个预览框显示在提问框中,您可以在其中预览您的问题。) –

回答

0

jQuery,请帮助:你刚才用$.ajax(),并通过POST您的数据发送给像save_my_xml.php。有一些类可以在PHP中使用XML,但是如果您与Smarty相处得很好,我建议您fetch您的xml_template.tpl,然后file_put_contents

+0

感谢您的回复,但我必须使用纯JavaScript来完成任务。 – Ved

+0

雅得到了解决方案...... !!! cgi-bin可以用来执行任务。 – Ved

相关问题