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;                 
        }
    ;
    }

    相关函数如下:
    相关函数

  • 相关阅读:
    ThinkPHP-5.0.23新的RCE漏洞测试和POC
    利用DNS日志进行MySQL盲注
    Prometheus自动发现
    relabel_config
    Maven安装
    Redis安装
    Node.js安装
    MySQL-5.7安装
    设计模式【1】------>单例模式
    Java设计模式概念以及六大设计原则
  • 原文地址:https://www.cnblogs.com/litsword/p/1174038.html
Copyright © 2011-2022 走看看