So I tested the following:
Copy Code code as follows:
var stringtodom=function (text) {
var doc;
if (window. ActiveXObject) {
doc = new ActiveXObject ("MSXML2. DOMDocument ");
Doc.loadxml (text). documentelement;
} else {
Doc = (new Domparser ()). parsefromstring (Text, "Text/xml");
}
return doc;
}
var xmldoc=stringtodom ("<body><a href= ' a ' >a</a><a href= ' B ' >b</a></body>"),
C
D1=new Date ();
for (Var i=0;i<100000;i++) {
C=xmldoc.getelementsbytagname ("a");
}
document.write ("getElementsByTagName:", New Date ()-d1);
D1=new Date ();
try{
for (Var i=0;i<100000;i++) {
C=xmldoc.selectnodes ("a");
}
document.write ("<br/>selectnodes:", New Date ()-d1);
}catch (ex) {document.write ("<br/>error:" +ex)}
Under IE selectnodes is still much faster,
Can FF but not this method, Google, find a way to use xpathevaluator to achieve, the following is a concrete implementation, but the efficiency is not very ideal:
Copy Code code as follows:
if (!window. ActiveXObject) {
(function () {
var oevaluator=new xpathevaluator (), Oresult;
XMLDocument.prototype.selectNodes = function (Sxpath) {
Oresult = Oevaluator.evaluate (Sxpath, this, NULL, xpathresult.ordered_node_iterator_type, NULL);
var anodes = [];
if (Oresult!= null) {
var oelement = Oresult.iteratenext ();
while (oelement) {
Anodes[anodes.length]=oelement;
Oelement = Oresult.iteratenext ();
}
}
return anodes;
}
})()
}
Evaluate (XPathExpression, ContextNode, Namespaceresolver, resulttype, result);
Returns an xpathresult based in an XPath expression and other given parameters.
XPathExpression is a string representing the XPath to be evaluated.
CONTEXTNODE Specifies the context node for the query (the [Http://www.w3.org/TR/xpath XPath specification). It ' s common to pass document as the context node.
Namespaceresolver is a function that would be passed any namespace prefixes and should return a string representing the Nam Espace URI associated with that prefix. It'll be used to resolve prefixes within the XPath itself and so that they can is matched with the document. Null is common to HTML documents or when no namespace prefixes are used.
Resulttype is ' An integer ' corresponds to ' the type of result Xpathresult to return. Use named constant properties, such as Xpathresult.any_type, of the Xpathresult constructor, which correspond to integers from 0 to 9.
The result is a existing xpathresult to the results. Null is the most common and would create a new Xpathresult
Complete test page:
Copy Code code as follows:
<!doctype html>
<title>selectNodes&getElementsByTagName</title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "Sohighthesky"/>
<meta name= "Keywords" content= "SelectNodes vs getElementsByTagName"/>
<body>
</body>
<script type= "Text/javascript" >
/*
*author:sohighthesky--Http://www.cnblogs.com/sohighthesky
*content:selectnodes vs getElementsByTagName
*/
if (!window. ActiveXObject) {
(function () {
var oevaluator=new xpathevaluator (), Oresult;
XMLDocument.prototype.selectNodes = function (Sxpath) {
Oresult = Oevaluator.evaluate (Sxpath, this, NULL, xpathresult.ordered_node_iterator_type, NULL);
var anodes = [];
if (Oresult!= null) {
var oelement = Oresult.iteratenext ();
while (oelement) {
Anodes[anodes.length]=oelement;
Oelement = Oresult.iteratenext ();
}
}
return anodes;
}
XMLDocument.prototype.selectSingleNode = function (Sxpath) {
Oresult = Oevaluator.evaluate (Sxpath, this, NULL, xpathresult.first_ordered_node_type, NULL);
First_ordered_node_type returns the "the" the "the".
return oresult==null?null:oresult.singlenodevalue;
}
})()
}
var stringtodom=function (text) {
var doc;
if (window. ActiveXObject) {
doc = new ActiveXObject ("MSXML2. DOMDocument ");
Doc.loadxml (text). documentelement;
} else {
Doc = (new Domparser ()). parsefromstring (Text, "Text/xml");
}
return doc;
}
var xmldoc=stringtodom ("<body><a href= ' a ' >a</a><a href= ' B ' >b</a></body>"),
C
D1=new Date ();
for (Var i=0;i<100000;i++) {
C=xmldoc.getelementsbytagname ("a");
}
document.write ("getElementsByTagName:", New Date ()-d1);
D1=new Date ();
try{
for (Var i=0;i<100000;i++) {
C=xmldoc.selectnodes ("a");
}
document.write ("<br/>selectnodes:", New Date ()-d1);
}catch (ex) {document.write ("<br/>error:" +ex)}
/*
var n=xmldoc.selectsinglenode ("body/a"), Doc=xmldoc.selectsinglenode ("body");//alert (N.childnodes[0].nodevalue)
for (Var i=0;i<10000;i++) {
Doc.appendchild (N.clonenode (True))
}
D1=new Date ();
C=xmldoc.getelementsbytagname ("a");
document.write ("<br/>getelementsbytagname:", New Date ()-d1);
D1=new Date ();
C=xmldoc.selectnodes ("a");
document.write ("<br/>selectnodes:", New Date ()-d1);
*/
</script>