In this post under JAXP, I will explain how to add custom error handler when parsing xml document through SAX api.
To provie a custom error handler, we need to implement the interface org.xml.sax.ErrorHandler as shown below
package sax;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class CustomErrorHandler implements ErrorHandler {
@Override
public void error(SAXParseException saxParseException) throws SAXException {
System.out.println("Hi Error: " + saxParseException.getMessage());
}
@Override
public void fatalError(SAXParseException saxParseException) throws SAXException {
System.out.println("Hi Fatal: " + saxParseException.getMessage());
}
@Override
public void warning(SAXParseException saxParseException) throws SAXException {
System.out.println("Hi Warning: " + saxParseException.getMessage());
}
}
The interface has three method error, fatalError, and warning, for which we have to provide implementation.
warning method is executed when SAX parser has to report warnings. After the method execution the parser will continue parsing the remaining document.
error method is executed when SAX parser has to report errors. After the method execution the parser will continue parsing the remaining document.
fatalError method is executed when SAX parser has to report fatal error which prevents further processing of the document.
Next we set an instance of CustomErrorHandler to SAX reader as shown below
If we don’t provide custom error handler to SAX reader, the errors and warning will go unreported except fatal errors that are thrown as SAXException.
Main Class
1 package sax;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5
6 import org.xml.sax.InputSource;
7 import org.xml.sax.SAXException;
8 import org.xml.sax.XMLReader;
9 import org.xml.sax.helpers.XMLReaderFactory;
10
11 public class SaxDemo2 {
12 public static void main(String[] args) throws SAXException, IOException {
13 FileInputStream fis = new FileInputStream("example3.xml");
14 InputSource is = new InputSource(fis);
15
16 ContentParser contentParser = new ContentParser();
17 CustomErrorHandler contentErrorHandler = new CustomErrorHandler();
18
19 XMLReader reader = XMLReaderFactory.createXMLReader();
20 reader.setContentHandler(contentParser);
21 reader.setErrorHandler(contentErrorHandler);
22 reader.parse(is);
23 }
24 }
In the above code, at line 17 we create an instance of CustomErrorHandler and set it to XMLReader instance at line 21 using “setErrorHandler” method.
Below is the document the code parses
example3.xml
<employee xmlns:e="http://www.example.org/employee">
<e:id>1</e:id>
<e:firstname>Jason</e:firstname>
<e:lastname>Bourne</e:lastname>
<e:position>Software Engineer</e:position>
<e:location>Fairfax
</employee>
Output
Locator set: com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy@5c647e05
Start Document at : 1, 1
Start Prefix: e at : 1, 53
Start Element: employee at : 1, 53
characters []
Start Element: e:id at : 2, 8
characters []
End Element: id at : 2, 16
characters []
Start Element: e:firstname at : 3, 15
characters []
End Element: firstname at : 3, 34
characters []
Start Element: e:lastname at : 4, 14
characters []
End Element: lastname at : 4, 33
characters []
Start Element: e:position at : 5, 14
characters []
End Element: position at : 5, 44
characters []
Start Element: e:location at : 6, 14
characters [Fairfax
]
Hi Fatal: The element type “e:location” must be terminated by the matching end-tag “”.
Exception in thread “main” org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 3; The element type “e:location” must be terminated by the matching end-tag “”.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at sax.SaxDemo2.main(SaxDemo2.java:22)