日期:2014-05-16  浏览次数:20391 次

javascript操作xml(增删改查)例子代码

文件stu.hta代码如下:

<html>
<head>
<title> 数据岛的显示 </title>
<style type="text/css">
#findPanel
{
    position:absolute;
    width:220px;
    border:1px solid #666666;
}

#findPanelTitle
{
    height:10px;
    background-color:#336699;
    cursor:move;
}

#findPanelContent
{
    padding:5px 5px 5px 5px;
    background-color:#6699CC;
}
</style>

</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--

    var   xmlDoc   =   new   ActiveXObject("Msxml2.DOMDocument");   
    xmlDoc.async   =   false;   
    xmlDoc.resolveExternals   =   false;   
    xmlDoc.load("stu.xml");
	//alert(xmlDoc.xml);
    //显示数据
	function show(){
		var vbo = document.getElementById("s").value;
		if(vbo=="显示学生信息"){
		   document.getElementById('info').style.visibility='visible';
		   document.getElementById("s").value="隐藏学生信息";
		 }else{
		    document.getElementById('info').style.visibility='hidden';
			document.getElementById("s").value="显示学生信息";
		 }
	}
	//查找
	function que(){
	   document.getElementById("ad").style.visibility="visible";
	   document.getElementById("qmd").style.visibility="visible";
	 }

     /**
      * 解析XML文件函数
      * @param xmlDoc XML对象
      * @param name 属性名称 格式如:user.name
      */
	function getXMLProperty(xmlDoc, name) {
     var keys = name.split('.');
     var node = xmlDoc.documentElement;
     for(var i=0; i<keys.length; i++) {
      var childs = node.childNodes;
      var key = keys[i];
      for(var k=0; k<childs.length; k++) {
        var child = childs[k];
        if(child.nodeName == key) {
          if(child.childNodes.length == 1) {
           return child.text;
           } else {
            node = child;
            break;
           }
       }
     }
   }
   return "";
  }
  //返回父节点(通过节点名字和节点值)
   function getNod(nam,val){
   	  var node = xmlDoc.documentElement;
	  var childs = node.childNodes;	  
	  for (var i=0;i<childs.length ;i++ )
	  {
	   var child = childs[i];
	   var childms = child.childNodes;
	   for(var k=0;k<childms.length;k++){
	   var childm = childms[k];
	    if(childm.nodeName == nam && (childm.text).substring(0,6)==val){
	   	 //alert(val);
		 return child;
	    }
	   }
	  }
	  return '';
   }
   //通过父节点和子节点名返回子节点值
   function getNodVal(nods,nam){
	   var childms = nods.childNodes;
	   for(var k=0;k<childms.length;k++){
	   var childm = childms[k];
	    if(childm.nodeName == nam){
	   	 //alert(childm.text);
		 return childm.text;
	    }
	   }
	  
	  return '';
   }
   //通过父节点和子节点名返回子节点
   function getCurNod(nods,nam){
	   var childms = nods.childNodes;
	   for(var k=0;k<childms.length;k++){
	   var childm = childms[k];
	    if(childm.nodeName == nam){
	   	 //alert(childm.text);
		 return childm;
	    }
	   }
	  
	  return '';
   }

	//开始查找
	function  query(){
	   var renum = /^\d{5}$/;
	   if(renum.exec(xh.value)){
	   if(xmlDoc.selectSingleNode("/学生管理[学生/学号="+xh.value+"]")){
		     var nod = getNod('学号',xh.value);
	         //getNodVal(nod,'姓名');
	     	 //getNodVal(nod,'性别');
		     //getNodVal(nod,'籍贯');
		   /*document.getElementById("xm").value=xmlDoc.selectSingleNode("/学生管理[学生/学号="+xh.value+"]").selectSingleNode("学生/姓名").text;
		   document.getElementById("xb").value=xmlDoc.selectSingleNode("/学生管理[学生/学号="+xh.value+"]").selectSingleNode("学生/性别").text;
		   document.getElementById("jg").value=xmlDoc.selectSingleNode("/学生管理[学生/学号="+xh.value+"]").selectSingleNode("学生/籍贯").text;
		   document.getElementById("xh").disabled="true";	*/
		   document.getElementById("xm").value=getNodVal(nod,'姓名');
		   document.getElementById("xb").value= getNodVal(nod,'性别');
		   document.getElementById("jg").value= getNodVal(nod,'籍贯');
		 }else{
		  alert("该学号还没有被占用!");
		 }
	   }else{
	    alert("学号非法!学号5位数字!");
		return false;
	   }
	}
	//修改
	function mod(){	 
	 	if(info_check()){
		  if(xmlDoc.selectSingleNode("/学生