本文實例講述了JS實現兼容各浏覽器解析XML文檔數據的方法。分享給大家供大家參考。具體分析如下:
網站上很多用JS解析XML文檔的資料或多或少都有點問題,
以下是自己總結的代碼,用來解析XML文檔,兼容各個浏覽器。
parseXMLDOM.js代碼:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 /* * 純JS解析XML文檔(兼容各個浏覽器) */ function parseXMLDOM(){ var _browserType = ""; var _xmlFile = ""; var _XmlDom = null; return { "getBrowserType" : function(){ return _browserType; }, "setBrowserType" : function(browserType){ _browserType = browserType; }, "getXmlFile" : function(){ return _xmlFile; }, "setXmlFile" : function(xmlFile){ _xmlFile = xmlFile; }, "getXmlDom" : function(){ return _XmlDom; }, "setXmlDom" : function(XmlDom){ _XmlDom = XmlDom; }, "getBrowserType" : function(){ var browserType = ""; if(navigator.userAgent.indexOf("MSIE") != -1){ browserType = "IE"; }else if(navigator.userAgent.indexOf("Chrome") != -1){ browserType = "Chrome"; }else if(navigator.userAgent.indexOf("Firefox") != -1){ browserType = "Firefox" } return browserType; }, "createXmlDom" : function(xmlDom){ if(this.getBrowserType() == "IE"){//IE浏覽器 xmlDom = new ActiveXObject('Microsoft.XMLDOM'); xmlDom.async = false; xmlDom.load(this.getXmlFile()); }else{ var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", this.getXmlFile(), false); xmlhttp.send(null); xmlDom = xmlhttp.responseXML; } return xmlDom; }, "parseXMLDOMInfo" : function(){ var xmlDom = this.getXmlDom(); if(this.getBrowserType() == "IE"){ var bookObj = xmlDom.selectNodes("books/book"); if(typeof(bookObj) != "undifined"){ var strHtml=""; for(var i = 0; i < bookObj.length; i++){ strHtml += bookObj[i].selectSingleNode("isbn").text; strHtml += " "; strHtml += bookObj[i].selectSingleNode("price").text; strHtml += " "; strHtml += bookObj[i].selectSingleNode("title").text; if(i != bookObj.length - 1){ strHtml += "<br>"; } } } }else{ var book = xmlDom.getElementsByTagName("book"); var strHtml=""; for(var i = 0;i < book.length;i++){ strHtml += book[i].getElementsByTagName("isbn")[0].textContent; strHtml += " "; strHtml += " "; strHtml += book[i].getElementsByTagName("price")[0].textContent; strHtml += " "; strHtml += book[i].getElementsByTagName("title")[0].textContent; if(i != book.length - 1){ strHtml += "<br>"; } } } document.getElementById("msg").innerHTML = strHtml; } } } window.onload = function(){ var parseObj = new parseXMLDOM(); //設置浏覽器類型 parseObj.setBrowserType(parseObj.getBrowserType()); //設置文件路徑 parseObj.setXmlFile("test.xml"); //創建XMLDOM parseObj.setXmlDom(parseObj.createXmlDom(null)); //解析XMLDOM parseObj.parseXMLDOMInfo(); }index.html代碼:
?
1 2 3 4 5 6 7 8 9 10 11 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JS解析XML文檔中的數據(兼容所有浏覽器)</title> <script language="javascript" type="text/javascript" src="js/parseXMLDOM.js"></script> </head> <body> <span id="msg"></span> </body> </html>test.xml代碼:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0" encoding="UTF-8"?> <books> <book> <isbn>2207-1258-123</isbn> <price>25</price> <title>Javascript</title> </book> <book> <isbn>2207-1258-456</isbn> <price>50</price> <title>Ajax</title> </book> <book> <isbn>2207-1258-789</isbn> <price>75</price> <title>C#</title> </book> </books>希望本文所述對大家的javascript程序設計有所幫助。