js里各浏览器解析XML,支持IE、火狐、Chrome等

@date:2014-09-15 22:05:00

js在chrome中加载XML,js加载XML支持ff,IE6+,Opera等浏览器

 

见代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js里各浏览器解析XML,支持IE、火狐、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);
            }
        </span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;">{
            xmlDoc.async</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #0000ff;">false</span><span style="background-color: #f5f5f5; color: #000000;">;
            xmlDoc.load(file);</span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">chrome没有load方法</span>

}catch(e){
//针对Chrome,不过只能通过http访问,通过file协议访问会报错
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open(
"GET",file,false);
xmlhttp.send(
null);
xmlDoc
= xmlhttp.responseXML.documentElement;
}
return xmlDoc;
}

    </span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> ob</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">loadxmlDoc(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">stu.xml</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
    </span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> stu</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">ob.getElementsByTagName(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">stu</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
    </span><span style="background-color: #f5f5f5; color: #0000ff;">for</span><span style="background-color: #f5f5f5; color: #000000;">(</span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> i</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">;i</span><span style="background-color: #f5f5f5; color: #000000;">&lt;</span><span style="background-color: #f5f5f5; color: #000000;">stu.length;i</span><span style="background-color: #f5f5f5; color: #000000;">++</span><span style="background-color: #f5f5f5; color: #000000;">){
        </span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;">  data</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">stu[i].childNodes;
        </span><span style="background-color: #f5f5f5; color: #0000ff;">for</span><span style="background-color: #f5f5f5; color: #000000;">(</span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> j</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">;j</span><span style="background-color: #f5f5f5; color: #000000;">&lt;</span><span style="background-color: #f5f5f5; color: #000000;">data.length;j</span><span style="background-color: #f5f5f5; color: #000000;">++</span><span style="background-color: #f5f5f5; color: #000000;">){
            </span><span style="background-color: #f5f5f5; color: #0000ff;">if</span><span style="background-color: #f5f5f5; color: #000000;">(data[j].nodeType</span><span style="background-color: #f5f5f5; color: #000000;">==</span><span style="background-color: #f5f5f5; color: #000000;">1</span><span style="background-color: #f5f5f5; color: #000000;">){
                </span><span style="background-color: #f5f5f5; color: #0000ff;">if</span><span style="background-color: #f5f5f5; color: #000000;">(document.all)
                    document.write(data[j].text</span><span style="background-color: #f5f5f5; color: #000000;">+</span><span style="background-color: #f5f5f5; color: #000000;">"</span> <span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);</span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">IE</span>
                <span style="background-color: #f5f5f5; color: #0000ff;">else</span><span style="background-color: #f5f5f5; color: #000000;">
                    document.write(data[j].textContent</span><span style="background-color: #f5f5f5; color: #000000;">+</span><span style="background-color: #f5f5f5; color: #000000;">"</span> <span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
            }
        }
        document.write(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">&lt;br/&gt;</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
    }
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">script</span><span style="color: #0000ff;">&gt;</span>

</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>

 

觉得不错,推荐一下哦!

Build by Loppo 0.6.14