推荐:  位移传感器  压力传感器  奥运五环  Trade Laws  capture color  北京奥运会 

在FireFox中实现selectNodes() 和 selectSingleNode() 方法

所属分类: 行业热门 > 电脑  关键词: selectNodes


在FireFox中实现selectNodes() 和 selectSingleNode() 方法
作者:admin 日期:2005-11-30
字体大小: 小 中 大  
最近在做个旅游网站,其中有个功能是查询合作单位的酒店预定系统,对方只提供了一个接口,及XML的格式,参考了一下别的网站的,只有IE下的代码,在FireFox下运行有问题,继而发现这个东东,实现就是应用的Ajax来实现的。合作单位是国外的公司,看来老外还是应用为先啊,我们还在炒作概念时,对方已经在实用了。

  其中有用到selectSingleNode函数,但这个在FireFox下不支持,遂用Google搜索了一下,找到一篇老外的文章,讲的是在FireFox下实现selectNodes 和 selectSingleNode 方法。先收录一下。(俺的英文很差,借助KingDict勉强能看懂,不过对于程序员来说,最重要的是代码,记得在某个Blog上看到,有个朋友买了本日文的Ajax入门,但却不懂日文,呵呵,完全是看代码来领会)

  老外的原文如下:

selectNodes()


 程序代码

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }

   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}




selectSingleNode()


 程序代码


// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var xItems = this.selectNodes(cXPathString, xNode);
      if( xItems.length > 0 )
      {
         return xItems[0];
      }
      else
      {
         return null;
      }
   }
   
   // prototying the Element
   Element.prototype.selectSingleNode = function(cXPathString)
   {    
      if(this.ownerDocument.selectSingleNode)
      {
         return this.ownerDocument.selectSingleNode(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}




例子:

XML文档


 程序代码

<root><complex><node><test>value&nbsp;1</test></node></complex><complex><node><test>value&nbsp;2</test></node></complex><complex><node><test>value&nbsp;3</test></node></complex><complex><node><test>value&nbsp;4</test></node></complex></root>



用法:


 程序代码

function test( oXML )
{
   var xItems = oXML.responseXML.selectNodes( "//complex/node/test/text()");
   var sn = "XPath : //complex/node/test/text() \nMethod : selectNodes()\n";
   for( var i = 0; i < xItems.length; i++ )
   {
      sn += "index : "+ i + " | value : " + xItems[i].nodeValue + "\n";
   }
   alert( sn );
   ssn = "XPath : //complex/node/test/text() \nMethod : selectSingleNode()\n";
   ssn+= oXML.responseXML.selectSingleNode( "//complex/node/test/text()").nodeValue;
   alert( ssn );
}


开放行业词条:在FireFox中实现selectNodes() 和 selectSingleNode() 方法
精彩分类推荐:休闲娱乐 小本创业 机械及行业设备 外贸专区 生活 化工