What is XML DOM parse error
Posted in
XML | DTD July 10, 2019
In This topic you will read about DOM parse error in XML.
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, "<");
document.write(errStr);
}
else
{
document.write("XML document is valid");
}
|
Take the intension at the XML file: Sevral_error.xml
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:
- Microsoft XML document creates in first line
- Then asynchronized loading , it make sure that parser will not continue execution of the script until whole document is not loaded.
- Then the third line tells the parser to load an XML document called "Sevral_error.xml"
- If parse error object is different from "0", then give the error and exit the function
- Return XML document. If the error code property is "0"
Example explained -Firefox
- The first line of coding creates an empty XML document object.
- Then asynchronized loading , it make sure that parser will not continue execution of the script until whole document is not loaded.
- Then the third line tells the parser to load an XML document called "Sevral_error.xml"
- If the returned document is an error document, alert the error and exit the function
- 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