XML DOM Parser Error
When Firefox encounters a parser error, it loads an XML document containing the error.
Parser Errors in Firefox
A parser error (parser-error) may occur when you attempt to open an XML document.
Unlike Internet Explorer, if Firefox encounters an error, it loads an XML document containing the error description.
The root node of the XML error document is named "parsererror". This is used to check for errors.
XML Error
In the following code, we will make the parser load a malformed XML document.
(You can read more about well-formed and valid XML in our XML Tutorial.)
Example
View XML File: note_error.xml
Example Explanation:
- Load the XML file
- Check if the root node's node name is "parsererror"
- Load the error string into the variable "errStr"
- Replace the "<" character with "<" before writing the error string as HTML
Note: In reality, only Internet Explorer checks your XML with a DTD, Firefox does not.
Cross-Browser Error Checking
Here, we create an XML loading function to check for parser errors in both Internet Explorer and Firefox:
Example
View XML File: note_error.xml
Example Explanation - Internet Explorer:
- The first line creates an empty Microsoft XML document object.
- The second line turns off asynchronous loading to ensure the parser does not continue executing the script until the document is fully loaded.
- The third line instructs the parser to load the XML document named "note_error.xml".
- If the ErrorCode property of the parseError object is not "0", alert the error and exit the function.
- If the ErrorCode property is "0", return the XML document.
Example Explanation - Firefox:
- The first line creates an empty XML document object.
- The second line turns off asynchronous loading to ensure the parser does not continue executing the script until the document is fully loaded.
- The third line instructs the parser to load the XML document named "note_error.xml".
- If the returned document is an error document, alert the error and exit the function.
- If not, return the XML document.