XML.prototype.XML_parseXMLStr = function()

in asdoc/XML.js [789:887]


XML.prototype.XML_parseXMLStr = function(xml) {
  xml = xml.replace(XML.xmlRegEx, "&");
  if (!XML.parser)
    XML.parser = new DOMParser();
  var /** @type {Document} */ doc;
  if (XML.errorNS == null) {
    try {
      XML.errorNS = XML.parser.parseFromString('<', 'application/xml').getElementsByTagName("parsererror")[0].namespaceURI;
    } catch (err) {
      XML.errorNS = "na";
    }
  }
  var /** @type {string} */ decl = org.apache.royale.utils.Language.string(XML.xmlDecl.exec(xml));
  if (decl)
    xml = xml.replace(decl, '');
  if (XML.ignoreWhitespace)
    xml = xml.trim();
  if (XML._defaultNS)
    xml = '<parseRoot xmlns="' + XML._defaultNS + '">' + xml + '</parseRoot>';
  else
    xml = '<parseRoot>' + xml + '</parseRoot>';
  try {
    doc = XML.parser.parseFromString(xml, "application/xml");
  } catch (err) {
    throw err;
  }
  var /** @type {NodeList} */ errorNodes = doc.getElementsByTagNameNS(org.apache.royale.utils.Language.string(XML.errorNS), 'parsererror');
  if (errorNodes.length > 0)
    throw new Error(errorNodes[0].innerHTML);
  doc = doc.childNodes[0];
  var /** @type {number} */ childCount = (doc.childNodes.length) >>> 0;
  var /** @type {boolean} */ foundRoot;
  var /** @type {number} */ foundCount = 0;
  for (var /** @type {number} */ i = 0; i < childCount; i++) {
    var /** @type {Node} */ node = doc.childNodes[i];
    if (node.nodeType == 1) {
      if (foundRoot) {
        foundCount++;
        break;
      }
      foundRoot = true;
      foundCount = 1;
      if (foundCount != 0) {
        this.resetNodeKind();
      }
      this._name = XML.getQName(node.localName, node.prefix, node.namespaceURI, false);
      XML._internal = true;
      XML.iterateElement(node, this);
      XML._internal = false;
    } else {
      if (foundRoot)
        continue;
      if (node.nodeType == 7) {
        if (XML.ignoreProcessingInstructions) {
          if (!foundCount) {
            delete this._name;
            this.setValue('');
          }
        } else {
          this.XML__nodeKind = XML.PROCESSING_INSTRUCTION;
          this.setName(node.nodeName);
          this.setValue(node.nodeValue);
          foundCount++;
        }
      } else if (node.nodeType == 4) {
        if (!foundCount) {
          delete this._name;
          this.setValue('<![CDATA[' + node.nodeValue + ']]>');
        }
        foundCount++;
      } else if (node.nodeType == 8) {
        delete this._name;
        if (XML.ignoreComments) {
          if (!foundCount) {
            this.setValue('');
          }
        } else {
          if (!foundCount) {
            this.XML__nodeKind = XML.COMMENT;
            this.setValue(node.nodeValue);
          }
          foundCount++;
        }
      } else if (node.nodeType == 3) {
        var /** @type {boolean} */ whiteSpace = !!(XML.isWhitespace.test(node.nodeValue));
        if (!whiteSpace || !XML.ignoreWhitespace) {
          if (!foundCount) {
            delete this._name;
            this.setValue(node.nodeValue);
          }
          foundCount++;
        } else {
        }
      }
    }
  }
  if (foundCount > 1)
    throw new TypeError('Error #1088: The markup in the document following the root element must be well-formed.');
};