Read XML file using JavaScript in Chrome
我需要使用JavaScript加载和读取XML文件。
以下代码可在Firefox,IE和Opera中正常运行:
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 | function loadXMLDoc(dname) { var xmlDoc // Internet Explorer try { xmlDoc = new ActiveXObject('Microsoft.XMLDOM') } catch (e) { // Firefox, Opera, etc. try { xmlDoc = document.implementation.createDocument('', '', null) } catch (e) { alert(e.message) } } try { xmlDoc.async = false xmlDoc.load(dname) return xmlDoc } catch (e) { alert(e.message) } return null } |
但是在Chrome中执行此代码会给我这个错误:
Object# has no method"load"
旧版代码
尽可能使用
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 | function loadXMLSync(url) { try { // Prefer XMLHttpRequest when available var xhr = new XMLHttpRequest() xhr.open('GET', url, false) xhr.setRequestHeader('Content-Type', 'text/xml') xhr.send() return xmlhttp.responseXML } catch (e) { // XMLHttpRequest not available, fallback on ActiveXObject try { var activex = new ActiveXObject('Microsoft.XMLDOM') activex.async = false activex.load(url) return activex } catch (e) { // Neither XMLHttpRequest or ActiveXObject are available return undefined } } } |
现代浏览器
如果您以现代浏览器(> IE6)为目标,则只需使用XMLHttpRequest:
1 2 3 4 5 6 7 8 9 | function loadXMLSync(url) { var xhr = new XMLHttpRequest() xhr.open('GET', url, false) xhr.setRequestHeader('Content-Type', 'text/xml') xhr.send() return xhr.responseXML } |
在MDN上,有使用XMLHttpRequest的指南。 但是从DOMImplementation.createDocument尚不清楚,直到您深入研究返回类型并看到Google Chrome不支持XMLDocument为止。 W3Schools上的示例使用XMLHttpRequest。
加
1 2 3 4 5 | var xhr = new XMLHttpRequest(); xhr.open("GET","/example/xdom/books.xml", false); xhr.send(null); xmlDoc = xhr.responseXML.documentElement; return xmlDoc; |
在
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 | function loadXMLDoc(dname) { var xmlDoc // Internet Explorer try { xmlDoc = new ActiveXObject('Microsoft.XMLDOM') } catch (e) { // Firefox, Opera, etc. try { xmlDoc = document.implementation.createDocument('', '', null) } catch (e) { alert(e.message) } } try { xmlDoc.async = false xmlDoc.load(dname) return xmlDoc } catch (e) { //alert(e.message) // For Chrome var xhr = new XMLHttpRequest(); xhr.open("GET","/example/xdom/books.xml", false); xhr.send(null); xmlDoc = xhr.responseXML.documentElement; return xmlDoc; } return null } |
遵循此操作以打印,加载,追加xml数据。此处xml以字符串形式存储在javascript中。此方法适用于chrome,firefox希望它也可以在其他方法中使用
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 | txt="<papers>"+"<paper>"+ "athor name</author>"+ "title"+ "<path>path</path>"+ "<track>which tack</track>"+ "</paper>"+ "<paper>"+ "athor name</author>"+ "title"+ "<path>path</path>"+ "<track>which tack</track>"+ "</paper>"+ "<paper>"+ "athor name</author>"+ "title"+ "<path>path</path>"+ "<track>which tack</track>"+ "</paper>"+ "<papers>"; if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(txt); } x=xmlDoc.getElementsByTagName("paper"); for (var i = 0; i < x.length; i++) { var athor =x[i].childNodes[0].firstChild.nodeValue; var title = x[i].childNodes[1].firstChild.nodeValue; var path = x[i].childNodes[2].firstChild.nodeValue; var tack =x[i].childNodes[3].firstChild.nodeValue; //do something with these values... //each iteration gives one paper details var xml=document.getElementById("element_id");// var li = document.createElement("br");// create a new newlink = document.createElement('A'); // creating an element newlink.innerHTML = athor;// adding athor value here newlink.setAttribute('href', path);// newlink.appendChild(li);// athor document.getElementById("element_id").appendChild(newlink); //finaly it becomes athor } |
我在这里发布了这个答案