The ContentHandler Interface

ContentHandler, shown in stripped-down form in Example 20-1, is an interface in the org.xml.sax package. You implement this interface in a class of your own devising. Next, you configure an XMLReader with an instance of your implementation. As the XMLReader reads the document, it invokes the methods in this object to tell your program what’s in the XML document. You can respond to these method invocations in any way you see fit.


The ContentHandler class has no relation to the moribund class. However, you may encounter a name conflict if you import both* and org.xml.sax.* in the same class. It’s better to import just the classes you actually need, rather than the entire package.

Example 20-1. The org.xml.sax.ContentHandler interface
package org.xml.sax; public interface ContentHandler { public void setDocumentLocator(Locator locator); public void startDocument( ) throws SAXException; public void endDocument( ) throws SAXException; public void startPrefixMapping(String prefix, String uri) throws SAXException; public void endPrefixMapping(String prefix) throws SAXException; public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts) throws SAXException; public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException; public void characters(char[ ] text, int start, int length) throws SAXException; public void ignorableWhitespace(char[ ...

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.