js解析xml浏览器兼容性处理

时间:2021-02-11 17:21:02
 /******************************************************************************
说明:xml解析类
******************************************************************************/ function XMLDOCDocument() {
this.xmlDoc = null;
this.async = false;
this.xml = null;
}; function createDocument() {
if (typeof arguments.callee.activeXString != "string") {
var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"],
i, len;
for (i = 0, len = versions.length; i < len; i++) {
try {
new ActiveXObject(versions[i]);
arguments.callee.activeXString = versions[i];
ActiveXObject(arguments.callee.activeXString);
break;
} catch (ex) {
//跳过
}
}
}
return new ActiveXObject(arguments.callee.activeXString);
};
/*
描述:加载xml
*/
XMLDOCDocument.prototype.loadXML = function (xmlInfo) {
//判断浏览器的类型
//支持IE浏览器 var isLoad = false;
var xmlDomVersions = ['MSXML.2.DOMDocument.6.0', 'MSXML.2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
for (var i = 0; i < xmlDomVersions.length; i++) {
try {
this.xmlDoc = new ActiveXObject(xmlDomVersions[i]);
this.xmlDoc.async = this.async;
this.xmlDoc.loadXML(xmlInfo); //loadXML方法载入xml字符串
this.xml = this.xmlDoc.documentElement.outerHTML;
isLoad = true;
break;
} catch (e) {
}
}
if (!isLoad) {
if (typeof DOMParser != "undefined") {
this.xmlDoc = (new DOMParser()).parseFromString(xmlInfo, "text/xml");
this.xml = this.xmlDoc.documentElement.outerHTML;
var errors = this.xmlDoc.getElementsByTagName("parsererror");
if (errors.length) {
throw new Error("XML parsing error:" + errors[0].textContent);
}
} else {
throw new Error("No XML parser available.");
}
}
}; /*
说明:选择所有符合条件的节点
*/
XMLDOCDocument.prototype.selectNodes = function (xPath) {
return this.xmlDoc.selectNodes(xPath);
};
/*
说明:选择一个符合条件的节点
*/
XMLDOCDocument.prototype.selectSingleNode = function (xPath) {
return this.xmlDoc.selectSingleNode(xPath);
}; /*
说明:创建新节点
*/
XMLDOCDocument.prototype.createElement = function (nodeName) {
return this.xmlDoc.createElement(nodeName);
}; /*
说明:创建新属性
*/
XMLDOCDocument.prototype.createAttribute = function (attrName) {
return this.xmlDoc.createAttribute(attrName);
};
/*
说明:获得xml字符串
*/
XMLDOCDocument.prototype.getXml = function () {
//return this.xmlDoc.xml;
if (typeof this.xmlDoc.xml != "undefined") { return this.xmlDoc.xml;
}
else if (typeof XMLSerializer != "undefined") {
return (new XMLSerializer()).serializeToString(this.xmlDoc);
} else {
throw new Error("Could not serialize XML DOM.");
}
};
/*
说明:获得xml字符串
*/
XMLDOCDocument.prototype.toString = function () {
//return this.xmlDoc.xml;
if (typeof this.xmlDoc.xml != "undefined") { return this.xmlDoc.xml;
}
else if (typeof XMLSerializer != "undefined") {
return (new XMLSerializer()).serializeToString(this.xmlDoc);
} else {
throw new Error("Could not serialize XML DOM.");
}
}; // check for XPath implementation
if (document.implementation.hasFeature("XPath", "3.0")) {
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function (cXPathString, xNode) {
if (!xNode) {
xNode = this;
}
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for (var i = 0; i < aItems.snapshotLength; i++) {
aResult[i] = aItems.snapshotItem(i);
}
return aResult;
}; // prototying the Element
Element.prototype.selectNodes = function (cXPathString) {
if (this.ownerDocument.selectNodes) {
return this.ownerDocument.selectNodes(cXPathString, this);
}
else {
throw "For XML Elements Only";
}
}; // prototying the XMLDocument
XMLDocument.prototype.selectSingleNode = function (cXPathString, xNode) {
if (!xNode) { xNode = this; }
var xItems = this.selectNodes(cXPathString, xNode);
if (xItems.length > 0) {
return xItems[0];
}
else {
return null;
}
}; // prototying the Element
Element.prototype.selectSingleNode = function (cXPathString) {
if (this.ownerDocument.selectSingleNode) {
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else { throw "For XML Elements Only"; }
} Element.prototype.__defineGetter__("innerText",
function () {
return this.textContent;
}
); Element.prototype.__defineSetter__("innerText",
function (sText) {
this.textContent = sText;
}
); };