为FireFox的XMLDocument 增加 LoadXML,SelectNodes,SelectSingleNode方法。

用惯了IE 的Dom,再来用 FireFox的Dom,感觉真的非常不习惯。连最起码的LoadXML方法都没有。
加入下面的代码,你就可以像在IE下一样使用XmlDom啦! 14.gif


None.gif var  infoNodes;
None.gif
if (document.all)
None.gif    infoNodes 
=  document.getElementById( " xmlInfo " ).XMLDocument.documentElement.selectNodes( " Product " );
ExpandedBlockStart.gifContractedBlock.gif
else dot.gif {
InBlock.gif    XMLDocument.prototype.loadXML 
= function(xmlString)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var childNodes = this.childNodes;
InBlock.gif        
for (var i = childNodes.length - 1; i >= 0; i--)
InBlock.gif            
this.removeChild(childNodes[i]);
InBlock.gif
InBlock.gif        
var dp = new DOMParser();
InBlock.gif        
var newDOM = dp.parseFromString(xmlString, "text/xml");
InBlock.gif        
var newElt = this.importNode(newDOM.documentElement, true);
InBlock.gif        
this.appendChild(newElt);
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
InBlock.gif    
// check for XPath implementation
InBlock.gif
    if( document.implementation.hasFeature("XPath""3.0") )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
// prototying the XMLDocument
InBlock.gif
       XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if!xNode ) dot.gif{ xNode = this; } 
InBlock.gif          
var oNSResolver = this.createNSResolver(this.documentElement)
InBlock.gif          
var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
InBlock.gif                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
null)
InBlock.gif          
var aResult = [];
InBlock.gif          
forvar i = 0; i < aItems.snapshotLength; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif             aResult[i] 
=  aItems.snapshotItem(i);
ExpandedSubBlockEnd.gif          }

InBlock.gif          
return aResult;
ExpandedSubBlockEnd.gif       }

InBlock.gif
InBlock.gif       
// prototying the Element
InBlock.gif
       Element.prototype.selectNodes = function(cXPathString)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif          
if(this.ownerDocument.selectNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif             
return this.ownerDocument.selectNodes(cXPathString, this);
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockStart.gifContractedSubBlock.gif          
elsedot.gif{throw "For XML Elements Only";}
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// check for XPath implementation
InBlock.gif
    if( document.implementation.hasFeature("XPath""3.0") )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
// prototying the XMLDocument
InBlock.gif
       XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if!xNode ) dot.gif{ xNode = this; } 
InBlock.gif          
var xItems = this.selectNodes(cXPathString, xNode);
InBlock.gif          
if( xItems.length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif             
return xItems[0];
ExpandedSubBlockEnd.gif          }

InBlock.gif          
else
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif             
return null;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif       }

InBlock.gif       
InBlock.gif       
// prototying the Element
InBlock.gif
       Element.prototype.selectSingleNode = function(cXPathString)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{    
InBlock.gif          
if(this.ownerDocument.selectSingleNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif             
return this.ownerDocument.selectSingleNode(cXPathString, this);
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockStart.gifContractedSubBlock.gif          
elsedot.gif{throw "For XML Elements Only";}
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif  
// 创建 XML 文档对象
InBlock.gif
  var xmlRef = document.implementation.createDocument("text/xml"""null);
InBlock.gif  
// 使用 importNode 将HTML DOM 的一部分转换为XML 文档。
InBlock.gif
  // 参数 true 表示克隆全部子元素。
InBlock.gif
  var myNode = document.getElementById("xmlInfo");
InBlock.gif  xmlRef.loadXML(myNode.innerHTML);
InBlock.gif  infoNodes 
= xmlRef.documentElement.childNodes;
ExpandedBlockEnd.gif}
posted on 2005-12-03 18:33 嘻哈呵嘿 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/skyover/archive/2005/12/03/290055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值