我的代码无法在xml文件中添加节点。请帮忙............... [英] my code is unable to add node in xml file. please help...............

查看:110
本文介绍了我的代码无法在xml文件中添加节点。请帮忙...............的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE html>
<html>
  <head>
    
    <script type="text/javascript">
	
	  function addElement()
	  {
		var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
		var root;
		var newNode;
		xmlDoc.async = false;
		xmlDoc.resolveExternals = false;
		xmlDoc.load("user.xml");

		try
		       {   
				   root = xmlDoc.documentElement;
				   alert(root.xml);
				   
				    var n1 =document.getElementById("t1").value;
				    var p1 =document.getElementById("t2").value;
				    newNode = xmlDoc.createElement("user");
                
				    var name =xmlDoc.createNode(1, "name", n1);
					var pass = xmlDoc.createNode(1, "pass", p1);
					
                    newNode.appendChild(name);
				    newNode.appendChild(pass);
					
				   
				    root.appendChild(newNode);
				  
				 
				 
				 
				 
				   alert(root.xml);
				   
				
				 xmlDoc.save("user.xml");
				 document.WriteString(root.lastChild.nodeValue);
				   alert("added");
				   
				}
		catch(e)
				 {
				   alert("error");
				 }
		}
		
	</script>
	 
	  </head>
	  <body>
		   <form id="user_form" method="post">
			  <table>
				<tr>
				<td>Name :</td><td><input type="text" id="t1"></td>
				</tr>
				<tr>
				<td>Password :</td><td><input type="text" id ="t2"></td>
				</tr>
				<tr>
				 <td><input type ="Submit" id="b1"  önclick="addElement()"></td>
				</tr>
			  </table>
		   </form>
	  </body>
</html>

推荐答案

嘿,

请添加一行代码xmlDoc.DocumentElement.AppendChild(newNode);就在xmlDoc.save(user.xml)之前;





现在你的代码就像:



........



Hey,
please add a line of code xmlDoc.DocumentElement.AppendChild(newNode); just before xmlDoc.save("user.xml");


Now your code will be like:

........

xmlDoc.DocumentElement.AppendChild(newNode);
xmlDoc.save("user.xml");





........



........


这篇关于我的代码无法在xml文件中添加节点。请帮忙...............的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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