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.

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).

3. Fatal Errors (fatalError(SAXParseException exception))

A fatal error indicates a failure of XML well-formedness rules or an unrecoverable system issue.

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