标签:
1 /****************************************************************************** 2 说明:xml解析类 3 ******************************************************************************/ 4 5 6 function XMLDOCDocument() { 7 this.xmlDoc = null; 8 this.async = false; 9 this.xml = null; 10 }; 11 12 13 function createDocument() { 14 if (typeof arguments.callee.activeXString != "string") { 15 var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"], 16 i, len; 17 for (i = 0, len = versions.length; i < len; i++) { 18 try { 19 new ActiveXObject(versions[i]); 20 arguments.callee.activeXString = versions[i]; 21 ActiveXObject(arguments.callee.activeXString); 22 break; 23 } catch (ex) { 24 //跳过 25 } 26 } 27 } 28 return new ActiveXObject(arguments.callee.activeXString); 29 }; 30 /* 31 描述:加载xml 32 */ 33 XMLDOCDocument.prototype.loadXML = function (xmlInfo) { 34 //判断浏览器的类型 35 //支持IE浏览器 36 37 var isLoad = false; 38 var xmlDomVersions = [‘MSXML.2.DOMDocument.6.0‘, ‘MSXML.2.DOMDocument.3.0‘, ‘Microsoft.XMLDOM‘]; 39 for (var i = 0; i < xmlDomVersions.length; i++) { 40 try { 41 this.xmlDoc = new ActiveXObject(xmlDomVersions[i]); 42 this.xmlDoc.async = this.async; 43 this.xmlDoc.loadXML(xmlInfo); //loadXML方法载入xml字符串 44 this.xml = this.xmlDoc.documentElement.outerHTML; 45 isLoad = true; 46 break; 47 } catch (e) { 48 } 49 } 50 if (!isLoad) { 51 if (typeof DOMParser != "undefined") { 52 this.xmlDoc = (new DOMParser()).parseFromString(xmlInfo, "text/xml"); 53 this.xml = this.xmlDoc.documentElement.outerHTML; 54 var errors = this.xmlDoc.getElementsByTagName("parsererror"); 55 if (errors.length) { 56 throw new Error("XML parsing error:" + errors[0].textContent); 57 } 58 } else { 59 throw new Error("No XML parser available."); 60 } 61 } 62 }; 63 64 /* 65 说明:选择所有符合条件的节点 66 */ 67 XMLDOCDocument.prototype.selectNodes = function (xPath) { 68 return this.xmlDoc.selectNodes(xPath); 69 }; 70 /* 71 说明:选择一个符合条件的节点 72 */ 73 XMLDOCDocument.prototype.selectSingleNode = function (xPath) { 74 return this.xmlDoc.selectSingleNode(xPath); 75 }; 76 77 78 /* 79 说明:创建新节点 80 */ 81 XMLDOCDocument.prototype.createElement = function (nodeName) { 82 return this.xmlDoc.createElement(nodeName); 83 }; 84 85 /* 86 说明:创建新属性 87 */ 88 XMLDOCDocument.prototype.createAttribute = function (attrName) { 89 return this.xmlDoc.createAttribute(attrName); 90 }; 91 /* 92 说明:获得xml字符串 93 */ 94 XMLDOCDocument.prototype.getXml = function () { 95 //return this.xmlDoc.xml; 96 if (typeof this.xmlDoc.xml != "undefined") { 97 98 return this.xmlDoc.xml; 99 } 100 else if (typeof XMLSerializer != "undefined") { 101 return (new XMLSerializer()).serializeToString(this.xmlDoc); 102 } else { 103 throw new Error("Could not serialize XML DOM."); 104 } 105 }; 106 /* 107 说明:获得xml字符串 108 */ 109 XMLDOCDocument.prototype.toString = function () { 110 //return this.xmlDoc.xml; 111 if (typeof this.xmlDoc.xml != "undefined") { 112 113 return this.xmlDoc.xml; 114 } 115 else if (typeof XMLSerializer != "undefined") { 116 return (new XMLSerializer()).serializeToString(this.xmlDoc); 117 } else { 118 throw new Error("Could not serialize XML DOM."); 119 } 120 }; 121 122 123 124 125 // check for XPath implementation 126 if (document.implementation.hasFeature("XPath", "3.0")) { 127 // prototying the XMLDocument 128 XMLDocument.prototype.selectNodes = function (cXPathString, xNode) { 129 if (!xNode) { 130 xNode = this; 131 } 132 var oNSResolver = this.createNSResolver(this.documentElement) 133 var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) 134 var aResult = []; 135 for (var i = 0; i < aItems.snapshotLength; i++) { 136 aResult[i] = aItems.snapshotItem(i); 137 } 138 return aResult; 139 }; 140 141 // prototying the Element 142 Element.prototype.selectNodes = function (cXPathString) { 143 if (this.ownerDocument.selectNodes) { 144 return this.ownerDocument.selectNodes(cXPathString, this); 145 } 146 else { 147 throw "For XML Elements Only"; 148 } 149 }; 150 151 // prototying the XMLDocument 152 XMLDocument.prototype.selectSingleNode = function (cXPathString, xNode) { 153 if (!xNode) { xNode = this; } 154 var xItems = this.selectNodes(cXPathString, xNode); 155 if (xItems.length > 0) { 156 return xItems[0]; 157 } 158 else { 159 return null; 160 } 161 }; 162 163 // prototying the Element 164 Element.prototype.selectSingleNode = function (cXPathString) { 165 if (this.ownerDocument.selectSingleNode) { 166 return this.ownerDocument.selectSingleNode(cXPathString, this); 167 } 168 else { throw "For XML Elements Only"; } 169 } 170 171 Element.prototype.__defineGetter__("innerText", 172 function () { 173 return this.textContent; 174 } 175 ); 176 177 Element.prototype.__defineSetter__("innerText", 178 function (sText) { 179 this.textContent = sText; 180 } 181 ); 182 183 };
标签:
原文地址:http://www.cnblogs.com/zynbg/p/5142672.html