XML ErrorHandler: Warnings, Errors, and Fatal Errors
The ErrorHandler interface in SAX and DOM parsers
provides a standardized mechanism for intercepting and managing issues
encountered during XML parsing and validation. By categorizing parsing
conditions into warnings, errors, and fatal errors, the interface allows
developers to customize how an application responds to different levels
of severity—ranging from minor schema non-conformities to structural
syntax violations.
The ErrorHandler Interface
In Java XML processing (JAXP), both SAX (XMLReader) and
DOM (DocumentBuilder) rely on the
org.xml.sax.ErrorHandler interface. When a parser
encounters an issue in an XML document, it invokes one of three callback
methods defined in this interface, passing a
SAXParseException that contains location details (line and
column numbers) and error descriptions.
1. Warnings
(warning(SAXParseException exception))
A warning indicates a minor problem or an informative condition that does not violate core XML well-formedness or strict validity constraints.
- Cause: Warnings typically represent conditions identified by the XML specification as non-critical, or parser-specific warnings such as redundant declarations, deprecations, or minor schema inconsistencies.
- Parser Behavior: The parser does not stop. Processing continues uninterrupted.
- Default Handling: Many default parser
configurations ignore warnings completely unless a custom
ErrorHandleris explicitly attached to log or handle them.
2. Errors
(error(SAXParseException exception))
An error indicates a violation of validity constraints as defined by the XML specification, a DTD, or an XML Schema (XSD).
- Cause: Typical causes include elements appearing in an invalid sequence, missing required attributes, or values that fail data type validation. The document is well-formed XML, but it does not conform to its defined grammar.
- Parser Behavior: By specification, standard errors are recoverable. The parser attempts to continue reading the document to detect and report additional validation errors in the remainder of the file.
- Default Handling: If no custom
ErrorHandleris supplied, non-fatal validity errors are often ignored or printed to standard output without halting execution. To treat validation errors as fatal, developers must explicitly rethrow theSAXParseExceptioninside theerrormethod.
3. Fatal
Errors (fatalError(SAXParseException exception))
A fatal error indicates a failure of XML well-formedness rules or an unrecoverable system issue.
- Cause: Examples include unclosed tags, improperly nested elements, illegal XML characters, undeclared entity references, or unexpected end-of-file tokens.
- Parser Behavior: The parser cannot guarantee the structural integrity of the document and must stop normal processing. While the parser may consume remaining bytes to report further syntax problems, it is prohibited by the XML standard from delivering regular parsing events to the application.
- Default Handling: A fatal error throws a
SAXParseExceptionimmediately, halting both SAX parsing streams and the construction of DOM document trees.
Summary of Differences
| Severity Level | Interface Method | Primary Cause | Parsing Action |
|---|---|---|---|
| Warning | warning() |
Minor non-validity issues, parser notices | Parsing continues normally |
| Error | error() |
Schema or DTD validity constraint violations | Parser attempts recovery to find more errors |
| Fatal Error | fatalError() |
Well-formedness violations, broken XML syntax | Parser immediately halts document processing |