varGetNodeValue=function(obj){varstr=””;if(window.ActiveXObject)//IE{str=obj.text;}else//Mozilla{try{str=obj.childNodes[0].nodeValue;}catch(ex){str=””;}}returnstr;}if(document.implementation&&document.implementation.createDocument){XMLDocument.prototype.loadXML=function(xmlString){varchildNodes=this.childNodes;for(vari=childNodes.length-1;i>=0;i–)this.removeChild(childNodes[i]);vardp=newDOMParser();varnewDOM=dp.parseFromString(xmlString,”text/xml”);varnewElt=this.importNode(newDOM.documentElement,true);this.appendChild(newElt);};// check for XPath implementationif(document.implementation.hasFeature(”XPath”,”3.0”)){// prototying the XMLDocumentXMLDocument.prototype.selectNodes=function(cXPathString,xNode){if(!xNode){xNode=this;}varoNSResolver=this.createNSResolver(this.documentElement)varaItems=this.evaluate(cXPathString,xNode,oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null)varaResult=[];for(vari=0;i<aItems.snapshotLength;i++){aResult[i]=aItems.snapshotItem(i);}returnaResult;}// prototying the ElementElement.prototype.selectNodes=function(cXPathString){if(this.ownerDocument.selectNodes){returnthis.ownerDocument.selectNodes(cXPathString,this);}else{throw”ForXMLElementsOnly”;}}}// check for XPath implementationif(document.implementation.hasFeature(”XPath”,”3.0”)){// prototying the XMLDocumentXMLDocument.prototype.selectSingleNode=function(cXPathString,xNode){if(!xNode){xNode=this;}varxItems=this.selectNodes(cXPathString,xNode);if(xItems.length>0){returnxItems[0];}else{returnnull;}}// prototying the ElementElement.prototype.selectSingleNode=function(cXPathString){if(this.ownerDocument.selectSingleNode){returnthis.ownerDocument.selectSingleNode(cXPathString,this);}else{throw”ForXMLElementsOnly”;}}}}
只要把以上代码存成一个 JS 文件,在页面上引用它,当 XML 节点的读取操作就可以像 IE 一样使用了,已经通过测试。