js 解析xml 浏览器兼容


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");

你可能感兴趣的:(xml,浏览器)