【转】两个小function实现XML和string相互转化

前端之家收集整理的这篇文章主要介绍了【转】两个小function实现XML和string相互转化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://www.cnblogs.com/Kymo/archive/2011/07/04/2097417.html


//convert string to xml object
function String2XML(xmlString) {
    // for IE
    if (window.ActiveXObject) {
      var xmlobject = new ActiveXObject("Microsoft.XMLDOM");
      xmlobject.async = "false";
      xmlobject.loadXML(xmlstring);
      return xmlobject;
    }
    // for other browsers
    else {
      var parser = new DOMParser();
      var xmlobject = parser.parseFromString(xmlstring,"text/xml");
      return xmlobject;
    }
  }
//convert xml object to string
function XML2String(xmlObject) {
    // for IE
    if (window.ActiveXObject) {       
      return xmlobject.xml;
    }
    // for other browsers
    else {        
      return (new XMLSerializer()).serializeToString(xmlobject);
    }
  }

猜你在找的XML相关文章