欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js 解析xml 浏览器兼容

程序员文章站 2022-06-13 10:02:44
...

function loadXMLDoc(dname) {
try //Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
catch (e) {
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc = document.implementation.createDocument("", "", null);
}
catch (e) { alert(e.message) }
}
try {
xmlDoc.async = false;
xmlDoc.load(dname);
return (xmlDoc);
}
catch (e) {

try { //Chrome 如果浏览器是Chrome,则会catch这个异常:Object # (a Document) has no method "load",所以,以下实现支持chrome加载XML文档
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", dname, false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML.documentElement;
return (xmlDoc);
} catch (e) {
alert(e.message);
}


}
return (null);
}



调用

xmlDoc = loadXMLDoc("upload/imgsXml.xml");