使用 javascript 遍历 xmlDom

<html>
<header><title>使用 javascript 遍历 xmlDom.</title></header>
<body>
<textarea id='log1' rows='20' cols='150'>
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <a/><b>ssssssss</b>
  </welcome-file-list>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <a/><b>ssssssss<a/><b>ssssssss</b></b>
  </welcome-file-list>		
</web-app>
</textarea>
<br><button onclick="parse1()"> parse1 </button>  
<button onclick="parse2()"> parse2 </button>
<br>
<textarea id='log2' rows='20' cols='150'></textarea>

<SCRIPT   LANGUAGE=javascript>
function parse1(){
  var xml = "";
  var xmlDoc = new   ActiveXObject("Microsoft.XMLDOM");
  var rootNode;
  //xmlDoc.load("a.xml");
  xmlDoc.loadXML( document.getElementById('log1').value );
  if(xmlDoc.readyState==4){
    if(xmlDoc.parseError.errorCode!=0){
      alert(xmlDoc.parseError.reason)
    } else {   
      rootNode = xmlDoc.documentElement;   
      xml = '';
      xml = scanXml(rootNode, xml, 0);
      document.getElementById('log2').value += "\n-----------\n";
      document.getElementById('log2').value += xml;
    }
  }
  return xml;
}

function parse2(){
  var xml = "";
  var xmlDoc = new   ActiveXObject("Microsoft.XMLDOM");
  var rootNode;
  //xmlDoc.load("a.xml");
  xmlDoc.loadXML( document.getElementById('log1').value );
  if(xmlDoc.readyState==4){
    if(xmlDoc.parseError.errorCode!=0){
      alert(xmlDoc.parseError.reason)
    } else {   
      rootNode = xmlDoc.selectSingleNode('web-app/welcome-file-list');
      xml = '';
      xml = scanXml(rootNode, xml, 0);
      document.getElementById('log2').value += "\n-----------\n";
      document.getElementById('log2').value += xml;
    }
  }
  return xml;
}

function nSpace( n ){
  var space ="";
  for(var i=0; i< n; i++){
    space += "--";
  }
  return space;
}   

function   scanXml(xmlNode, xml, level){
  var currXml = '';
  var currLevel = level + 1;  
  //alert( xmlNode.nodeType + ":" + xmlNode.nodeName );
  //nodeType 此属性只读且传回一个数值。有效的数值符合以下的型别: 
  switch( xmlNode.nodeType ){
    case 1: //-ELEMENT
      currXml += "\n" + nSpace( level ) + "<" + xmlNode.nodeName + ">";
      if(xmlNode.hasChildNodes()){
        var   nodeList   =   xmlNode.childNodes;
          for(var   i=0;i<nodeList.length;i++){
            currXml = scanXml(nodeList[i], currXml, currLevel);
          }
      }
      currXml += "\n" + nSpace( level ) + "</" + xmlNode.nodeName + ">";
      break;
    case 2: break;//-ATTRIBUTE 
    case 3: //-TEXT      
      currXml = "\n" + nSpace( level ) + xmlNode.nodeValue;
      break;
    case 4: break;//-CDATA 
    case 5: break;//-ENTITY REFERENCE 
    case 6: break;//-ENTITY 
    case 7: break;//-PI (processing instruction) 
    case 8: break;//-COMMENT 
    case 9: break;//-DOCUMENT 
    case 10: break;//-DOCUMENT TYPE 
    case 11: break;//-DOCUMENT FRAGMENT 
    case 12: break;//-NOTATION 
    default:
  }
  return xml + currXml;
}
</SCRIPT>  
</body>
</html>

你可能感兴趣的:(JavaScript,html,xml,Web,Microsoft)