faqts : Computers : Programming : Languages : JavaScript : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

100 of 132 people (76%) answered Yes
Recently 9 of 10 people (90%) answered Yes


How do I stream XML into JS on the fly without having an actual .xml file?
How do I parse a string with XML markup into an XML DOM document?

Mar 20th, 2005 05:29
Martin Honnen, Michael Kernahan, Nathan Morehart, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmthloadXML.asp http://www.xulplanet.com/references/objref/DOMParser.html

Depending on the browser (respectively the parser exposed to script) you
need different ways:
--- IE5+/Win: loadXML with MSXML -------------------------------------
If you want to parse a string with XML markup into an XML DOM document
then the XMLDOM ActiveX object of MSXML (as used in IE5+/Win or other
Windows script environments like ASP or WSH) provides the method loadXML
that takes the string with XML markup as the sole argument and returns
true or false depending on whether the markup has been successfully
parsed or not. If there has been an error then the parseError object
property of the XML document provides details on what went wrong:
  var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
  xmlDocument.async = false;
  var wellFormedMarkup = '<gods><god>Kibo</god></gods>';
  var loaded = xmlDocument.loadXML(wellFormedMarkup);
  if (loaded) {
    // shows 'gods'
  var notWellFormedMarkup = '<gods><god>Kibo</gods>';
  loaded = xmlDocument.loadXML(notWellFormedMarkup);
  if (!loaded) {
    alert(xmlDocument.parseError.reason +
    // shows error with end tag </gods> not matching start tag <god>
--- Mozilla (and Mozilla based browsers): DOMParser ------------------
Mozilla exposes the DOMParser constructor function to script to create a
DOM parser object that has method parseFromString that takes as the
first argument the string with the markup and as the second argument a
string with the content type (e.g. application/xml or text/xml) and then
returns an XML DOM document in any case, if the markup passed in is
well-formed then the document is the proper document parsed from that
markup, however if the markup passed in is not well-formed then
nevertheless an XML document is returned, only it is then containing
parse error information.
The following code snippet shows how to handle that:
  var domParser = new DOMParser();
  var wellFormedMarkup = '<gods><god>Kibo</god></gods>';
  var xmlDocument = domParser.parseFromString(wellFormedMarkup,
  var parseError = checkForParseError(xmlDocument);
  if (parseError.errorCode == 0) {
    // alerts 'gods'
  var notWellFormedMarkup = '<gods><god>Kibo</gods>';
  xmlDocument = domParser.parseFromString(notWellFormedMarkup,
  parseError = checkForParseError(xmlDocument);
  if (parseError.errorCode == 0) {
    // process XML document here
  else {
    alert(parseError.reason + '\r\n' + parseError.srcText);
    // shows detailed error message
  function checkForParseError (xmlDocument) {
    var errorNamespace =
    var documentElement = xmlDocument.documentElement;
    var parseError = { errorCode : 0 };
    if (documentElement.nodeName == 'parsererror' &&
        documentElement.namespaceURI == errorNamespace) {
      parseError.errorCode = 1;
      var sourceText =
documentElement.getElementsByTagNameNS(errorNamespace, 'sourcetext')[0];
      if (sourceText != null) {
        parseError.srcText = sourceText.firstChild.data
      parseError.reason = documentElement.firstChild.data;
    return parseError;