js解析XMl文件,兼容IE、Firefox、谷歌

[javascript] view plain copy

    loadXML = function(xmlFile){  
        var xmlDoc=null;  
        var agent = navigator.userAgent.toLowerCase();  
      
        //判断浏览器的类型  
        //支持IE浏览器  
        if(agent.indexOf("msie") > 0){  
            alert("22");  
            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]);  
                    break;  
                }catch(e){  
                }  
            }  
        }  
        //支持firefox浏览器  
         else if(agent.indexOf("firefox") > 0){  
            try{  
                xmlDoc = document.implementation.createDocument('','',null);  
            }catch(e){  
            }  
        } else{//谷歌浏览器  
            alert("111111111");  
            var oXmlHttp = new XMLHttpRequest() ;  
            oXmlHttp.open( "GET", xmlFile, false ) ;  
            oXmlHttp.send(null) ;   
            return oXmlHttp.responseXML;  
        }  
         if(xmlDoc!=null){  
            xmlDoc.async = false;  
            xmlDoc.load(xmlFile);  
        }   
        return xmlDoc;  
    }  
    $(function(){  
        var bankDoc = loadXML("bank.xml");    
        var list = bankDoc.getElementsByTagName("bank");  
        $("BankID").length = 0;  
        for (var i = 0; i < list.length; i++) {  
            bankId = list[i].getAttribute("bankId");  
            bankName = list[i].getAttribute("bankName");  
            alert(bankName);  
        }  
          
        });  

xml文件:

[html] view plain copy

    <?xml version="1.0" encoding="UTF-8" ?>  
    <Items>  
        <bank bankId="" bankName="无" />  
        <bank bankId="100" bankName="邮政储蓄银行" />  
        <bank bankId="102" bankName="中国工商银行" />  
        <bank bankId="103" bankName="中国农业银行" />  
        <bank bankId="104" bankName="中国银行" />  
        <bank bankId="105" bankName="中国建设银行" />  
        <bank bankId="301" bankName="交通银行" />  
        <bank bankId="302" bankName="中信银行" />  
        <bank bankId="303" bankName="中国光大银行" />  
        <bank bankId="304" bankName="华夏银行" />  
        <bank bankId="305" bankName="中国民生银行" />  
        <bank bankId="306" bankName="广东发展银行" />  
    </Items>  

亲测支持ie8、Firefox、chrome、手机qq浏览器。

 

转载自:http://blog.csdn.net/u283056051/article/details/49254777

posted @ 2016-04-29 17:17  居然已存在  阅读(214)  评论(0编辑  收藏  举报