js跨浏览器实现将字符串转化为xml对象的方法_javascript技巧
程序员文章站
2022-04-28 18:37:48
...
注意该死的ie多版本的问题
function loadXML(xmlString) {
var xmlDoc = null;
if (!window.DOMParser && window.ActiveXObject) {
var xmlDomVersions = ['MSXML.2.DOMDocument.6.0', 'MSXML.2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
for (var i = 0; i try {
xmlDoc = new ActiveXObject(xmlDomVersions[i]);
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
break;
} catch (e) {
}
}
}
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;
}
复制代码 代码如下:
function loadXML(xmlString) {
var xmlDoc = null;
if (!window.DOMParser && window.ActiveXObject) {
var xmlDomVersions = ['MSXML.2.DOMDocument.6.0', 'MSXML.2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
for (var i = 0; i try {
xmlDoc = new ActiveXObject(xmlDomVersions[i]);
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
break;
} catch (e) {
}
}
}
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;
}