What is XML DOM parse error

In This topic you will read about DOM parse error in XML.
  • 4492

XML DOM Parser Errors

XML parser error vary from browser to browser. When load XML document give the error

Parser Error in Firefox

Parser-error may occur when you trying to open the XML document

If firefox encounters an error , then it loads an XML document that contains the error description unlike the Internet Explorer
The root node of XML error document is "parseerror". It is used to check if any error occur

XML Error

The below given example  code the parse load an XML documents that is not a well-formed. That is why give the error property

xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load("Several_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 document is valid");
}

 

Take the intension at the XML file: Sevral_error.xml

  • First load the xml file

  • Then check that if the nodeName of the root node is "parsererror"

  • Variable "errStr" load the error string
     

Cross Browser Error Check

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

if (xmlDoc.parseError.errorCode != 0)
{
alert("Error in the line " + xmlDoc.parseError.line +
" in the position " + xmlDoc.parseError.linePos +
"\nError Code is: " + 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(docname);
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);
}
 

Example explained - Internet Explorer:

  1. Microsoft XML document creates in first line
  2. Then asynchronized loading , it make sure that parser will not continue execution of the script until whole document is not loaded.
  3. Then the third line tells the parser to load an XML document called "Sevral_error.xml"
  4. If parse error object is different from "0", then give the error and exit the function
  5. Return XML document. If the error code property is "0"

Example explained -Firefox

  1. The first line of coding creates an empty XML document object.
  2. Then asynchronized loading , it make sure that parser will not continue execution of the script until whole document is not loaded.
  3. Then the third line tells the parser to load an XML document called "Sevral_error.xml"
  4. If the returned document is an error document, alert the error and exit the function
  5. If not, return the XML document

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.