兼容加载Xml字符串

时间:2021-02-24 16:18:25
var _loadXML = function(xmlString){
var xmlDoc=null;
//支持IE浏览器
if(!window.DOMParser && window.ActiveXObject){ //window.DOMParser 判断是否是非ie浏览器
var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
for(var i=0;i<xmlDomVersions.length;i++){
try{
xmlDoc = new ActiveXObject(xmlDomVersions[i]);
xmlDoc.async = false;
xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
break;
}catch(e){
}
}
}
//支持Mozilla,Chrome浏览器
else if(window.DOMParser && document.implementation && document.implementation.createDocument){
try{ domParser = new DOMParser();
xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
}catch(e){
}
}
else{
return null;
} return xmlDoc;
}

直接调用  _loadXML 即可拿到节点!