javascript xml解析代碼

來源:互聯網
上載者:User

function WEIPXml(_url) {
 /** XML檔案的URL */
 this.url = _url;
 /** XML文檔對象 */
 this.oDocument = null;
 /** XMLHTTP對象 */
 this.oXmlHttp = null;

 /** 瀏覽器類型 */
 this.isNS = isNetscape;
 this.isIE = isIE;

 /**
  * 擷取XmlHttp對象
  */
 this.getXmlHttp = function() {
  if (this.isIE) {
   try {
    this.oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     this.oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
     // nothing
    }
   }
  } else if (window.XMLHttpRequest) {
   this.oXmlHttp = new XMLHttpRequest();
   //if (this.oXmlHttp.overrideMimeType) {
   // this.oXmlHttp.overrideMimeType("text/xml");
   //}
  }
 }
 
 /**
  * 裝載XML檔案
  */
 this.load = function(_content, _isAsynchronous, _mothed, _xmlUrl) {
  _isAsynchronous = (!_isAsynchronous) ? false : true;
  if (!this.oXmlHttp) {
   this.getXmlHttp();
  }
  if (_xmlUrl) {
   this.url = _xmlUrl;
  }
  if (!_mothed) {
   _mothed = 'POST';
  }
  this.oXmlHttp.open(_mothed, this.url, _isAsynchronous);
  if (_mothed == 'POST') {
   this.oXmlHttp.setRequestHeader('Content-Type',
             'application/x-www-form-urlencoded');
  }
  if (_content) {
   this.oXmlHttp.send(_content);
  } else {
   this.oXmlHttp.send(null);
  }
  if (!_isAsynchronous) {
   if (this.oXmlHttp.status == 200) {
    this.oDocument = this.oXmlHttp.responseXML;
    return true;
   } else {
    alert('XML 請求錯誤: ' + this.oXmlHttp.statusText
      + ' (' + this.oXmlHttp.status + ')');
   }
  } else {
   this.oXmlHttp.onreadystatechange = function() {
    switch (this.oXmlHttp.readyState) {
    case 0 : // 未初始化
     this.onUninitialized(); break;
    case 1 : // 裝載中
     this.onLoading(); break;
    case 2 : // 裝載完畢
     this.onLoaded(); break;
    case 3 : // 互動中
     this.onInteractive(); break;
    case 4 : // 完成
     if (this.oXmlHttp.status == 200) {
      this.oDocument = this.oXmlHttp.responseXML;
      this.onComplete();
     } else {
      alert('XML 請求錯誤: ' + this.oXmlHttp.statusText
          + ' (' + this.oXmlHttp.status + ')');
     }
     break;
    }
   }.bind(this);
  }
  return false;
 }
 
 this.onUninitialized = function() {}; // 未初始化
 this.onLoading   = function() {}; // 裝載中
 this.onLoaded   = function() {}; // 裝載完畢
 this.onInteractive  = function() {}; // 互動中
 this.onComplete   = function() {}; // 完成

 /**
  * 提取節點數組
  */
 this.selectNodes = function(xpath) {
  if (this.isIE) {
   return this.oDocument.selectNodes(xpath);
  } else {
   var aNodeArray = new Array();
   var xPathResult = this.oDocument.evaluate(xpath, this.oDocument,
     this.oDocument.createNSResolver(this.oDocument.documentElement),
     XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
   if (xPathResult) {
    var oNode = xPathResult.iterateNext();
    while (oNode) {
     aNodeArray[aNodeArray.length] = oNode;
     oNode = xPathResult.iterateNext();
    }
   }
   return aNodeArray;
  }
 }

 /**
  * 提取單個節點
  */
 this.selectSingleNode = function(xpath) {
  if (this.isIE) {
   return this.oDocument.selectSingleNode(xpath);
  } else {
   var xPathResult = this.oDocument.evaluate(xpath, this.oDocument,
     this.oDocument.createNSResolver(this.oDocument.documentElement), 9, null);
   if ( xPathResult && xPathResult.singleNodeValue ) {
    return xPathResult.singleNodeValue;
   } else {
    return null;
   }
  }
 }

 /**
  * 擷取節點text值
  */
 this.getText = function(xpath) {
  var oNode = (typeof(xpath) == "string") ? this.selectSingleNode(xpath) : xpath;
  if (oNode) {
   return (isIE) ? oNode.text : oNode.textContent;
  } else {
   return this.oXmlHttp.responseText;
  }
 }

 /**
  * 擷取節點屬性值
  */
 this.getAttribute = function(xpath, attributeName) {
  var oNode = (typeof(xpath) == "string") ? this.selectSingleNode(xpath) : xpath;
  if (oNode) {
   var oAttribute = oNode.attributes.getNamedItem(attributeName);
   return (oAttribute) ? oAttribute.value : null;
  } else {
   return null;
  }
 }
}

Function.prototype.bind = function() {
 var __method = this, args = $A(arguments), object = args.shift();
 return function() {
  return __method.apply(object, args.concat($A(arguments)));
 }
}

var $A = Array.from = function(iterable) {
 if (!iterable) return [];
 if (iterable.toArray) {
  return iterable.toArray();
 } else {
  var results = [];
  for (var i = 0; i < iterable.length; i++) {
   results.push(iterable[i]);
  }
  return results;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.