WEB开发网
开发学院网页设计JavaScript javascript之XML DOM对象 阅读

javascript之XML DOM对象

 2010-09-14 13:06:39 来源:WEB开发网   
核心提示: 三,提供一个跨浏览器的XML DOM对象解决方案,javascript之XML DOM对象(6),来自于《javascript高级程序设计》function XmlDom() {//通过对象/属性检测法,判断是IE来是Mozillaif (window.ActiveXObject) {va

三,提供一个跨浏览器的XML DOM对象解决方案,来自于《javascript高级程序设计》

function XmlDom() {
  //通过对象/属性检测法,判断是IE来是Mozilla
  if (window.ActiveXObject) {
    var arrSignatures = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0",
               "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument",
               "Microsoft.XmlDom"];
            
    for (var i=0; i < arrSignatures.length; i++) {
      try {
    
        var oXmlDom = new ActiveXObject(arrSignatures[i]);
      
        return oXmlDom;
    
      } catch (oError) {
        //ignore
      }
    }     
  
    throw new Error("MSXML is not installed on your system.");
     
  //同上     
  } else if (document.implementation && document.implementation.createDocument) {
    
    var oXmlDom = document.implementation.createDocument("","",null);
  
    //创建Mozilla版本的parseError对象
    oXmlDom.parseError = {
      valueOf: function () { return this.errorCode; },
      toString: function () { return this.errorCode.toString() }
    };
    
    //初始化parseError对象
    oXmlDom.__initError__();
        
    
    oXmlDom.addEventListener("load", function () {
      this.__checkForErrors__();
      this.__changeReadyState__(4);
    }, false);
  
    return oXmlDom;    
    
  } else {
    throw new Error("Your browser doesn't support an XML DOM object.");
  }
}
  
//此处用到了该书中一个浏览器系统检测js文件,如果是Mozilla
if (isMoz) {
  
  Document.prototype.readyState = 0;
  Document.prototype.onreadystatechange = null;
  
  Document.prototype.__changeReadyState__ = function (iReadyState) {
    this.readyState = iReadyState;
  
    if (typeof this.onreadystatechange == "function") {
      this.onreadystatechange();
    }
  };
  //初始化parseError对象
  Document.prototype.__initError__ = function () {
    this.parseError.errorCode = 0;
    this.parseError.filepos = -1;
    this.parseError.line = -1;
    this.parseError.linepos = -1;
    this.parseError.reason = null;
    this.parseError.srcText = null;
    this.parseError.url = null;
  };
  
  Document.prototype.__checkForErrors__ = function () {
  
    if (this.documentElement.tagName == "parsererror") {
  
      var reError = />([sS]*?)Location:([sS]*?)Line Number (d+), Column (d+):<sourcetext>([sS]*?)(?:-*^)/;
  
      reError.test(this.xml);
      
      this.parseError.errorCode = -999999;
      this.parseError.reason = RegExp.$1;
      this.parseError.url = RegExp.$2;
      this.parseError.line = parseInt(RegExp.$3);
      this.parseError.linepos = parseInt(RegExp.$4);
      this.parseError.srcText = RegExp.$5;
    }
  };
  
   //定义Mozilla的loadXML方法 
  Document.prototype.loadXML = function (sXml) {
  
    this.__initError__();
  
    this.__changeReadyState__(1);
  
    var oParser = new DOMParser();
    var oXmlDom = oParser.parseFromString(sXml, "text/xml");
    while (this.firstChild) {
      this.removeChild(this.firstChild);
    }
  
    for (var i=0; i < oXmlDom.childNodes.length; i++) {
      var oNewNode = this.importNode(oXmlDom.childNodes[i], true);
      this.appendChild(oNewNode);
    }
    
    //载入后检查错误
    this.__checkForErrors__();
    
    //没有问题,设置readyState属性为4
    this.__changeReadyState__(4);
  
  };
  
  Document.prototype.__load__ = Document.prototype.load;
  
  Document.prototype.load = function (sURL) {
    this.__initError__();
    this.__changeReadyState__(1);
    this.__load__(sURL);
  };
  
  Node.prototype.__defineGetter__("xml", function () {
    var oSerializer = new XMLSerializer();
    return oSerializer.serializeToString(this, "text/xml");
  });
  
}

上一页  1 2 3 4 5 6 

Tags:javascript XML DOM

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接