logo 西林@生存 - 骑猪闯天下!
手痒,把JavaScript遍历XML的程序拿出来SHOW SHOW
2006-12-7 16:07:00 By:xling
意志不坚,又被同志们拉下水,对JS手痒了!

先是让我改我的JTree,在是笨小孩让我看它的问题,汗,难道我要重出江湖不成?

下面是JS遍利XML文件各节点的示例,好久好久以前写的东东了!

 <script language="JavaScript" type="text/javascript">
 var xmlFile="http://xling.blueidea.com/rss2.xml";
 
 var createXMLDom=function(){
  if (window.ActiveXObject)
   var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
  else
   if (document.implementation&&document.implementation.createDocument)
    var xmldoc=document.implementation.createDocument("","doc",null);
  xmldoc.async = false;
  //为了和FireFox一至,这里不能改为False;
  xmldoc.preserveWhiteSpace=true;
  return xmldoc;
 }
 
 var createXMLHttp=function(){
  var xmlHttp;
  if (window.ActiveXObject){
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }else{
   xmlHttp=new XMLHttpRequest();
  }
  return xmlHttp;
 }
 
 
 //-----------------------------------------------------------------------------
 //加载XML文件。
 var xmlDom=createXMLDom();
 try{
  xmlDom.load(xmlFile);
 }catch(e){
  var xmlHttp=createXMLHttp();
   xmlHttp.onreadystatechange = function(){
    if(xmlHttp.readyState == 4){
     xmlDom=xmlHttp.responseXML;
    }else{
     //window.state="XML文件加载中...";
    }
   }  
   xmlHttp.open("GET",xmlFile,false);
   xmlHttp.send(null);
 }
 var DOMRoot=xmlDom.documentElement;
 //取出指定节点的属性。
 var getDOMAtt=function(pNode,pAttribute){
  try{
   return pNode.attributes.getNamedItem(pAttribute).nodeValue;
  }catch(e){
   //alert("指定节点不存在,或指定属性:"+pAttribute+" 不存在!")
   return false;
  }
  
 } 
 
 var attList=function(pNode){
  if(pNode.nodeType == 4) return '';
  var oAtt,tmpString="[ ";
  try{
   for(var i=0;oAtt=pNode.attributes[i];i++){
    tmpString+=oAtt.nodeName+" = '" + oAtt.nodeValue+"' ";
   }
   tmpString+=" ]"
   return tmpString;
  }catch(e){ return ''}
 }
 
 var getNodeName = function(pNode){
  if(pNode.nodeType == 4) return '';
  return pNode.nodeName;
 }
 
 var getNodeValue = function(pNode){
  try{
   return " : " + pNode.firstChild.nodeValue;
  }catch(e){return ''}
 }
 
 var nbsp=function(pNum){
  var tmpString=""
  for(var i=0;i<pNum;i++){
   tmpString+=" "//这里是全角的空格
  }
  return tmpString;
 }
 
 var switchTree=function(pNode,pLevel){
  var tNode,i;
  for(i=0;tNode=pNode.childNodes[i];i++){
   if(tNode.nodeType==3) continue;//因为preserveWhiteSpace等于true
   document.write(nbsp(pLevel),"<font color='blue'><b>",getNodeName(tNode),"</b></font><font color='red'>",attList(tNode),"</font>",getNodeValue(tNode),"<br />");
   switchTree(tNode,pLevel+1);
  }
 }
 
 switchTree(DOMRoot,2)
</script>
</html>

转我的贴,写你的名,是一种可耻的行为!请不要让我BS你!
阅读全文 | 回复(1) | 引用通告 | 编辑
Re:手痒,把JavaScript遍历XML的程序拿出来SHOW SHOW
2006-12-8 11:29:00 By:shuntianpeng
能否弄个详细的说明尼,我在努力读懂它,可还是有点困难~
转我的贴,写你的名,是一种可耻的行为!请不要让我BS你!
个人主页 | 引用 | 返回 | 删除 | 回复

发表评论:

    密码:
    主页:
    标题:
    页面数据正在载入...
<<  < 2007 - 12 >  >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Placard
1,長江後浪推前浪,前浪死在沙灘上!
2,有了钱的男人,才是真正的男人...
3,不要以为把你的小JJ埋进土里,你就rape了整个地球;不要以为把你的小JJ朝向天空,你就rape了整个宇宙!
Logon System
Search
Info about this blog
Others
myDream
bxna 京ICP备05002321号