Latest web development tutorials

XML DOM parser error

When Firefox parser encounters an error, it will load an XML document that contains the error.


Parser error in Firefox

When you try to open an XML document, it is a parser error (parser-error) may occur.

And Internet Explorer browsers, if Firefox encounters an error, it loads the XML document that contains the error described.

XML error document root name is "parsererror". This is used to check for errors.


XML error (XML Error)

In the following code, we will let the parser to load a bad form of XML documents.

(You can in our XML tutorial Read more about the good and effective form of XML.)

Examples

xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load("note_error.xml");

if (xmlDoc.documentElement.nodeName=="parsererror")
{
errStr=xmlDoc.documentElement.childNodes[0].nodeValue;
errStr=errStr.replace(/</g, "&lt;");
document.write(errStr);
}
else
{
document.write("XML is valid");
}

try it"

See XML file: note_error.xml

Examples explain:

  1. Loading XML file
  2. Check whether the root node name is "parsererror"
  3. The error string variable loading "errStr"
  4. Before the error string is written in HTML, the "<" character replaced by "& lt;"

Note: In fact, only Internet Explorer will check your XML with DTD, Firefox will not.


Cross-browser error checking

Here, we create a XML loader function, check parser error in Internet Explorer and Firefox:

Examples

function loadXMLDocErr(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(dname);

if (xmlDoc.parseError.errorCode != 0)
{
alert("Error in line " + xmlDoc.parseError.line +
" position " + xmlDoc.parseError.linePos +
"nError Code: " + xmlDoc.parseError.errorCode +
"nError Reason: " + xmlDoc.parseError.reason +
"Error Line: " + xmlDoc.parseError.srcText);
return(null);
}
}
catch(e)
{
try //Firefox
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load(dname);
if (xmlDoc.documentElement.nodeName=="parsererror")
{
alert(xmlDoc.documentElement.childNodes[0].nodeValue);
return(null);
}
}
catch(e) {alert(e.message)}
}
try
{
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}

try it"

See XML file: note_error.xml

Examples explain - Internet Explorer:

  1. The first line creates an empty Microsoft XML document object.
  2. The second line turns off asynchronous loading, to ensure that the parser will not continue execution of the script before the document is fully loaded.
  3. The third line tells the parser to load an XML document called "note_error.xml" of.
  4. If ErrorCode property parseError object and "0" is different, and to remind the wrong exit.
  5. If ErrorCode property to "0", and returns an XML document.

Examples explain - Firefox:

  1. The first line creates an empty XML document object.
  2. The second line turns off asynchronous loading, to ensure that the parser will not continue execution of the script before the document is fully loaded.
  3. The third line tells the parser to load an XML document called "note_error.xml" of.
  4. If the return of the document is a false document, reminding error and exit.
  5. If not, it returns an XML document.