js异步读取xml(支持ff和xpath)

在各大论坛间google了一整天,东拼西凑的终于调试成功了。

首先,定义一个全局变量用于识别当前浏览器是否IE
全局变量

针对xml对象在Firefox里的不同属性进行转换,统一接口
if (!_isIe){
    
var  ex;
    XMLDocument.prototype.__proto__.__defineGetter__(
"xml" ,function (){
         
try {
             
return  new XMLSerializer().serializeToString(this);
        }
 catch (ex){
             
var  d  =  document.createElement("div");
            d.appendChild(
this.cloneNode( true ));
             
return  d.innerHTML;
        }

    }
);
    Element.prototype.__proto__.__defineGetter__(
"xml",  function (){
         
try {
             
return   new  XMLSerializer().serializeToString(this);
        }
 catch (ex){
             
var  d  =  document.createElement("div");
            d.appendChild(
this.cloneNode(true));
             
return  d.innerHTML;
        }

    }
);
    XMLDocument.prototype.__proto__.__defineGetter__(
"text",  function (){
         
return   this.firstChild.textContent;
    }
);
    Element.prototype.__proto__.__defineGetter__(
"text",  function (){
         
return   this.textContent;
    }
);

    XMLDocument.prototype.selectSingleNode 
= Element.prototype.selectSingleNode = function (XPath){
        alert(
"selectSingleNode");
         
var  x = this.selectNodes(xpath);
         alert(x);
         alert(x.length);
         
if ( !|| x.length < 1 ) return  null;
         
return  x[0];
    }

    XMLDocument.prototype.selectNodes 
= Element.prototype.selectNodes = function (xpath){       
     alert(
"selectNodes");
        
var xpe = new XPathEvaluator();
     alert(
"1");         
      
var nsResolver = xpe.createNSResolver(this.ownerDocument == null ? this.documentElement : this.ownerDocument.documentElement);       
         alert(
"2");    
           
var result = xpe.evaluate(xpath, this, nsResolver, 0null);
         alert(
"3");    
         
var  found =  [];
         
var  res;         
         
while (res = result.iterateNext())
        found.push(res);
        
return found;
    }

    
    Node.prototype.transformNode 
= function (oXslDom) 
                 
        
var oProcessor = new XSLTProcessor(); 
        oProcessor.importStylesheet(oXslDom); 
     
        
var oResultDom = oProcessor.transformToDocument(this); 
        
var sResult = oResultDom.xml; 
     
        
if (sResult.indexOf(" <transformiix:result">  -1
            sResult 
= sResult.substring(sResult.indexOf(""+ 1, sResult.lastIndexOf(" <")); 
        }
 
     
        
return sResult;                 
    }
;
}

相关函数如下:
相关函数

posted @ 2008-04-28 10:00  litsword  阅读(2734)  评论(1编辑  收藏  举报