February 20, 2002 - Printing A Parsing Error | WebReference

February 20, 2002 - Printing A Parsing Error

Yehuda Shiran February 20, 2002
Printing A Parsing Error
Tips: February 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

When loading XML files, you must keep error handling in place. Otherwise, debugging the XML, DTD, and XSL files is going to be very time consuming. The following script reads mydvd7.xml, prints a relevant error message if the parsing was not successful, and prints the DOM tree otherwise:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
var namedNodeMap;
xmlDoc.async = false;
xmlDoc.load("mydvd7.xml");
if (xmlDoc.parseError.errorCode != 0) {
  alert("errorCode: " + xmlDoc.parseError.errorCode + "\n" +
        "filepos: " + xmlDoc.parseError.filepos + "\n" +
        "line: " + xmlDoc.parseError.line + "\n" +
        "linepos: " + xmlDoc.parseError.linepos + "\n" +
        "reason: " + xmlDoc.parseError.reason + "\n" +
        "srcText: " + xmlDoc.parseError.srcText + "\n" +
        "url: " + xmlDoc.parseError.url);
} else {
    alert(xmlDoc.documentElement.xml);
  }