动态创建XML文档

 
       var createXML = function (str) {
          if (typeof DOMParser !== "undefined") {
            return (new DOMParser()).parseFromString(str, "application/xml");
          }else if (typeof ActiveXObject != "undefined") {
            if (typeof arguments.callee.activeXString !== "string"){//在createXML上设置一个静态成员
              var versions = ["MSXML2.DOMDocument.6.0",
                "MSXML2.DOMDocument.5.0",
                "MSXML2.DOMDocument.4.0",
                "MSXML2.DOMDocument.3.0",
                "MSXML2.DOMDocument",
                "Microsoft.XMLDOM"];
              for (var i=0,len=versions.length; i < len; i++){
                try {
                  var xml = new ActiveXObject(versions[i]);
                  arguments.callee.activeXString = versions[i];
                  xml = null;
                } catch (e){}
              }
            }
            xml = new ActiveXObject(arguments.callee.activeXString);
            xml.async="false";
            xml.loadXML(str);
            return xml
          }
        }

        var xml = createXML('<xml id="xx"><book><title>司徒正美</title></book></xml>');

你可能感兴趣的:(xml)