Even More Handlers
Now I want to show you two more handler classes that SAX offers. Both
of these interfaces are no longer part of the core SAX distribution,
and are located in the org.xml.sax.ext
package
to indicate they are extensions to SAX. However, most parsers (such
as Apache Xerces) include these two classes for use. Check your
vendor documentation, and if you don’t have these classes, you
can download them from the SAX web site. I warn you that not all SAX
drivers support these extensions, so if your vendor doesn’t
include them, you may want to find out why, and see if an upcoming
version of the vendor’s software will support the SAX
extensions.
LexicalHandler
The first of these two handlers is the most useful:
org.xml.sax.ext.LexicalHandler
.
This handler provides methods that can receive notification of
several lexical events such as comments, entity declarations, DTD
declarations, and CDATA sections. In
ContentHandler, these lexical events are
essentially ignored, and you just get the data and declarations
without notification of when or how they were provided.
This is not really a general-use handler, as most applications
don’t need to know if text was in a CDATA
section or not. However, if you are working with an XML editor,
serializer, or other component that must know the exact
format of the input document, not just its
contents, the LexicalHandler can really help you
out. To see this guy in action, you first need to add an import
statement for org.xml.sax.ext.LexicalHandler ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access