zoukankan      html  css  js  c++  java
  • 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;                 
        }
    ;
    }

    相关函数如下:
    相关函数

  • 相关阅读:
    day4
    cache用法
    Excel批量生成SQL语句,处理大量数据(增,改)
    IDEA中Maven依赖下载失败解决方案
    IDEA 自动生成类图 UML
    springboot报错说 Failed to parse multipart servlet request; nested exception is java.io.IOException
    controller层的引用service层一直报空指针问题
    CONCATENATE函数
    AQS
    String 类和常量池
  • 原文地址:https://www.cnblogs.com/litsword/p/1174038.html
Copyright © 2011-2022 走看看