文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>javascript 读取XML节点的文本

javascript 读取XML节点的文本

时间:2008-02-05  来源:shiwudao

对于类似下面的xml文件 <?xml version='1.0' ?> <root> <node> this is test   <subnode>      this is sub test   </subnode> </node> </root> 如果想得到<node>中包含的文本,一般在IE下使用node.text或者Mozilla/FF下使用textContent属性。但是在低版本的Mozilla1.5之下对于有<![CDATA[ ]]>包含的文本,或者FF下对于node下面的子节点的理解各不相同,不能够取出<node>下所有的文本,即    this is test         this is sub test   写了下面的通用方法,大家也可以根据需要进行修改以可以取出node下各个子节点的内容。 function getNodeText(node) {
  try {
   if(typeof(node.text) != 'undefined') {
     return normalize(node.text);
   }else {
     var str="";
     for(var i =0 ; i < node.childNodes.length; i++) {
       str += node.childNodes[i].textContent;
     }
     return normalize(str);
   }
  }catch(e) {
    return "";
  }
}
  function normalize(str) { //本函数为了去掉首尾的空格回车之类
  if(typeof(str) == 'undefined' || str==null) return null;
  else  return str.replace(/^\s*|\s*$/g, "");
}
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载