View Code
1 function XmlDom(){ 2 if(window.ActiveXObject){ 3 var arrSignatures = ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0", 4 "MSXML2.DOMDocument.3.0","MSXML2.DOMDocument", 5 "Microsoft.XmlDom"] 6 for(var i=0; i < arrSignatures.length;i++){ 7 try{ 8 var oXmlDom = new ActiiveXObject(arrSignatures[i]); 9 return oXmlDom; 10 } catch (oError){ 11 //ignore 12 } 13 } 14 15 throw new Error("MSXML is not installed on your system."); 16 } else if (document.implementation && document.implementation.createDocument){ 17 //DOM-specific code 18 var oXmlDom = document.implementation.createDocument("","",null); 19 oXmlDom.parseError = { 20 valueOf:function(){ return this.errorCode;}, 21 toString:function(){ return this.errorCode.toString();} 22 }; 23 oXmlDom._initError_(); 24 oXmlDom.addEventListener("load",function(){ 25 this._checkForError_(); 26 this._changeReadyState_(4); 27 },false); 28 return oXmlDom 29 } else { 30 throw new Error("Your browser doesn't support an XML DOM Object.") 31 } 32 } 33 if(isMoz){ 34 //mozilla 不支持 readyState 特性。 35 //对 Document 类进行一些改写使 Mozilla 支持 readystate 特性 以及 onreadystatechange 事件处理函数 36 37 Document.prototype.readyState = 0; 38 Document.prototype.onreadystatechange = null; 39 40 Document.prototype._changeReadyState_=function(iReadyState){ 41 this.readyState = iReadystate; 42 if(typeof this.onreadystatechange == "function"){ 43 this.onreadystatechange(); 44 } 45 //一旦readyState 特性发生变化 必须调用 onreadystatechange 函数。这样才能实时更新状态 46 }; 47 48 Document.prototype._initError_=function(){ 49 this.parseError.errorCode = 0; 50 this.parseError.filepos = -1; 51 this.parseError.line = -1; 52 this.parseError.linepos = -1; 53 this.parseError.reason = null; 54 this.parseError.srcText = null; 55 this.parseError.url = unll; 56 }; 57 58 Document.prototype._checkForErrors_=function(){ 59 if(this.documentElement.tagName == "parsererror"){ 60 var reError = />([\s\S]*?)Location:([\s\S]*?)LineNumber (\d+), Column(\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/; 61 reError.test(this.xml); 62 this.parseError.errorCode = -999999; 63 this.parseError.reason = RegExp.$1; 64 this.parseError.url = RegExp.$2; 65 this.parseError.line = parseInt(RegExp.$3); 66 this.parseError.linepos = parseInt(RegExp.$4); 67 this.parseError.srcText = RegExp.$5; 68 } 69 }; 70 71 Document.prototype.loadXml = function(sXml){ 72 this._initError_(); 73 //每次 readyState 更改时都要调用 onreadystatechange.如果在 parseError对象 中存在旧的数据,必须在调用 onreadystatechange 前重置,否则可能造成混淆。 74 this._changeReadyState_(1); 75 76 var oParser = new DOMParser(); 77 var oXmlDom = oParser.parseFromString(sXml,"text/xml"); 78 //Mozilla 的 XML DOM 不支持 loadXML() 方法。要将 XML 字符串解析为 DOM,必须使用 DOMParser 对像。 79 while(this.firstChild){ 80 this.removeChild(this.firstChild); 81 } 82 for (var i=0; i < oXmlDom.childNodes.length;i++){ 83 var oNewNode = this.importNode(oXmlDom.childNodes[i],true); 84 this.appendChild(oNewNode); 85 } 86 //原来的文档必须清空其内容,this 关键词指向XML DOM对象. //在删除所有的子节点后,所有的oXmlDom的子节点必须导入到文档中(使用ImportNode()方法)并作为子节点添加(使用appendChild()) 87 this._checkForErrors_(); 88 this._changeReadyState_(4); 89 }; 90 91 Document.prototype._load_=Document.prototype.load; 92 Document.prototype.load=function(sURL){ 93 this._initError_(); 94 this._changeReadyState_(1); 95 this._load_(sURL); 96 //mozilla 的 load() 方法和IE的Load() 工作方式一样。只要指定要载入的XML文件 97 }; 98 99 Node.prototype._defineGetter_("xml",function(){ 100 var oSerializer = new XMLSerializer(); 101 return oSerializer.serializeToString(this,"text/xml"); 102 //微软的XMLDOM 提供了xml 特性,Mozilla 提供了可以用于同样目的的XMLSerializer对象 103 //defineGetter()方法只存在于Mozilla中,用于为某个特性定义获取函数,也就是说读取特性时,就会调用这个函数并返回他的结果 104 //defineGetter() 是按照Js对于私有特性和方法的标准隐藏的 105 //oObject._defineGetter_("propertyName",function(){return "propertyValue"}) 106 }) 107 108 } 109 110 var oXmlDom = new XmlDom(); 111 oXmlDom.onreadystatechange = function(){ 112 if(oXmlDom.readyState == 4){ 113 if(oXmlDom.parseError != 0){ 114 var oError = oXmlDom.parseError; 115 alert("An error occurred :\n Error Code: " 116 + oError.errorCode + "\n" 117 + "Line:" + oError.line + "\n" 118 + "Line Pos:" + oError.linepos + "\n" 119 + "Reason:" +oError.reason); 120 } 121 } 122 }