如何为FireFox的XMLDocument增加LoadXML SelectNodes SelectSingleNode?
用惯了IE 的Dom,再来用FireFox的Dom,感觉真的非常不习惯。连最起码的LoadXML方法都没有。
加入下面的代码,你就可以像在IE下一样使用XmlDom啦!
varinfoNodes;
if(document.all)
infoNodes=document.getElementById("xmlInfo").XMLDocument.documentElement.selectNodes("Product");

else...{
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);
};

//checkforXPathimplementation
if(document.implementation.hasFeature("XPath","3.0"))

...{
//prototyingtheXMLDocument
XMLDocument.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;
}

//prototyingtheElement
Element.prototype.selectNodes=function(cXPathString)

...{
if(this.ownerDocument.selectNodes)

...{
returnthis.ownerDocument.selectNodes(cXPathString,this);
}

else...{throw"ForXMLElementsOnly";}
}
}

//checkforXPathimplementation
if(document.implementation.hasFeature("XPath","3.0"))

...{
//prototyingtheXMLDocument
XMLDocument.prototype.selectSingleNode=function(cXPathString,xNode)

...{

if(!xNode)...{xNode=this;}
varxItems=this.selectNodes(cXPathString,xNode);
if(xItems.length>0)

...{
returnxItems[0];
}
else

...{
returnnull;
}
}

//prototyingtheElement
Element.prototype.selectSingleNode=function(cXPathString)

...{
if(this.ownerDocument.selectSingleNode)

...{
returnthis.ownerDocument.selectSingleNode(cXPathString,this);
}

else...{throw"ForXMLElementsOnly";}
}
}

//创建XML文档对象
varxmlRef=document.implementation.createDocument("text/xml","",null);
//使用importNode将HTMLDOM的一部分转换为XML文档。
//参数true表示克隆全部子元素。
varmyNode=document.getElementById("xmlInfo");
xmlRef.loadXML(myNode.innerHTML);
infoNodes=xmlRef.documentElement.childNodes;
}