复杂xml字符串解析

时间:2022-10-31 13:03:32
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script type='text/javascript'>
    loadXML = function(xmlString){
        var xmlDoc=null;
        //判断浏览器的类型
        //支持IE浏览器 
        if(!window.DOMParser && window.ActiveXObject){   //window.DOMParser 判断是否是非ie浏览器
            var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
            for(var i=0;i<xmlDomVersions.length;i++){
                try{
                    xmlDoc = new ActiveXObject(xmlDomVersions[i]);
                    xmlDoc.async = false;
                    xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
                    break;
                }catch(e){
                }
            }
        }
        //支持Mozilla浏览器
        else if(window.DOMParser && document.implementation && document.implementation.createDocument){
            try{
                
                domParser = new  DOMParser();
                xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
            }catch(e){
            }
        }
        else{
            return null;
        }

        return xmlDoc;
    }

    //对xml对象进行判断
    checkXMLDocObj = function(xmlString){
        var xmlDoc = loadXML(xmlString);
        
        if(xmlDoc==null){
            alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!');
            //window.location.href='某某地址(比如首页)';
        }
        return xmlDoc;
    }
    
    //参数1:控件id号,参数2:理解为定位节点
    initializeSelect = function(oid,xPath){
        var xmlstr="<?xml version='1.0' encoding='utf-8'?>"+
        "<Organization>"+
        "<Department coding='001' name='TR4版本11月11号' modifytime='1388125467' MaxDepID='0' sn=''>"+
            "<Device id='1000002' />"+
            "<Channel id='1000002$1$0$0' />"+
            "<Channel id='1000002$1$0$1' />"+
            "<Channel id='1000002$1$0$2' />"+
            "<Channel id='1000002$1$0$3' />"+
        "</Department>"+
        "<Devices MaxDevID='1000'>"+
               "<Device id='1000002' type='65541' name='66' manufacturer='1' model='' ip='172.6.5.66' port='37777' user='admin' "+
               "password='admin' desc='' status='1' logintype='' regid='' proxyport='-1' unitnum='-1' deviceCN='' deviceSN='' deviceIp='172.6.5.66' devicePort='37777' devMaintainer='' devMaintainerPh='' rights='0'>"+
    "<UnitNodes index='0' channelnum='4' streamType='-1' subType='0' type='1'>"+
                       "<Channel id='1000002$1$0$0' name='66_1' desc='' status='0' channelType='1' channelSN='30001' rights='100000011001111' cameraType='1' CtrlId='30001' latitude='' longitude='' viewDomain='' />"+
                       "<Channel id='1000002$1$0$1' name='66_2' desc='' status='0' channelType='1' channelSN='30002' rights='100000011001111' cameraType='1' CtrlId='30002' latitude='' longitude='' viewDomain='' />"+
                       "<Channel id='1000002$1$0$2' name='66_3' desc='' status='0' channelType='1' channelSN='30003' rights='100000011001111' cameraType='1' CtrlId='30003' latitude='' longitude='' viewDomain='' />"+
                       "<Channel id='1000002$1$0$3' name='66_4' desc='' status='0' channelType='1' channelSN='30004' rights='100000011001111' cameraType='1' CtrlId='30004' latitude='' longitude='' viewDomain='' />"+
                "</UnitNodes>"+
            "</Device>"+
        "</Devices>"+
    "</Organization>";


        var xmlDoc = checkXMLDocObj(xmlstr);

        var n;
        var l;
        var e = document.getElementById(oid);//得到下拉列表框控件
        if(e!=null){
            n = xmlDoc.getElementsByTagName("Channel");
           
            l = n.length;
            
             for(var i=0;i<l;i++){
                //alert(n[i].attributes[0].text);
                alert(n[i].attributes[0].value);
            } 
            
            

        }
    }
  </script>
 </HEAD>

 <BODY onload="initializeSelect('company','Channel')">
  <select id='company' name='company' onChange='showValue()'>
    <option selected='selected'></option>
  </select>
 </BODY>
</HTML>

更多api查看

复杂xml字符串解析

 

http://www.w3school.com.cn/xmldom/prop_attr_value.asp