js裡各瀏覽器解析XML,支援IE、Firefox、Chrome等

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   strong   

js在chrome中載入XML,js載入XML支援ff,IE6+,Opera等瀏覽器

 

見代碼:

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>js裡各瀏覽器解析XML,支援IE、Firefox、Chrome等</title></head><body>    <script>        function loadxmlDoc(file){            try{                //IE                xmlDoc=new ActiveXObject("Microsoft.XMLDOM");            }catch(e){                ////Firefox, Mozilla, Opera, etc                xmlDoc=document.implementation.createDocument("","",null);            }            try{                xmlDoc.async=false;                xmlDoc.load(file);//chrome沒有load方法            }catch(e){                //針對Chrome,不過只能通過http訪問,通過file協議訪問會報錯                var xmlhttp = new window.XMLHttpRequest();                  xmlhttp.open("GET",file,false);                  xmlhttp.send(null);                  xmlDoc = xmlhttp.responseXML.documentElement;             }            return xmlDoc;        }        var ob=loadxmlDoc("stu.xml");        var stu=ob.getElementsByTagName("stu");        for(var i=0;i<stu.length;i++){            var  data=stu[i].childNodes;            for(var j=0;j<data.length;j++){                if(data[j].nodeType==1){                    if(document.all)                        document.write(data[j].text+" ");//IE                    else                        document.write(data[j].textContent+" ");                }            }            document.write("<br/>");        }    </script></body></html>

 

 

附stu.xml

<?xml version="1.0" encoding="UTF-8"?><stulist>    <stu>        <name>張三</name>        <age>20</age>        <sex>男</sex>        <classid>lamp87</classid>    </stu>    <stu>        <name>李四</name>        <age>22</age>        <sex>女</sex>        <classid>lamp86</classid>    </stu>    <stu>        <name>王五</name>        <age>21</age>        <sex>女</sex>        <classid>lamp87</classid>    </stu>     <stu>        <name>王五</name>        <age>21</age>        <sex>女</sex>        <classid>lamp87</classid>    </stu>    <stu>        <name>趙六</name>        <age>24</age>        <sex>男</sex>        <classid>lamp88</classid>    </stu></stulist>

 

覺得不錯,推薦一下哦!

js裡各瀏覽器解析XML,支援IE、Firefox、Chrome等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.