ie9,10下虽然报了
new window.DOMParser()
不兼容的错误,但是其实执行没问题的,如果项目要求兼容到ie8,如下写法:
// 解析xml兼容
function parseXml(xmlStr) {
var obj, xmlDoc;
if (typeof window.DOMParser != "undefined") {
obj = function (xmlStr) {
return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
};
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
obj = function (xmlStr) {
xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlStr);
return xmlDoc;
};
} else {
throw new Error("No XML parser found");
}
return obj(xmlStr);
}
var str = "<wrapper><success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/></wrapper>";
var xml = parseXml(str);
console.log(xml);
console.log(xml.documentElement.nodeName);