Understand How SAX Handles Entity Resolution
Another
basic building block of the SAX API is the process of entity
resolution. This process is handled through the
org.xml.sax.EntityResolver interface. Like the
aforementioned InputSource, the
EntityResolver interface is often overlooked and
ignored by SAX developers.
However, through the use of a solid
EntityResolver implementation, XML parsing speed
can be dramatically enhanced.
At its simplest, an EntityResolver tells a SAX
parser implementation how to look up resources specified in an XML
document (such as entity references). For example, take a look at the
following XML document fragment:
<entityContainer> <entity>&reference;</entity> </entityContainer>
This document fragment illustrates an entity reference named
reference. When a parser runs across this entity
reference, it begins the process of resolving that entity. The parser
will first consult the document’s DTD or XML schema
for a definition, like this:
<!ENTITY reference
PUBLIC " -//O'Reilly//TEXT Best Practices Reference//EN"
"reference.xml"
>From this, it gains both the public ID (-//O'Reilly//TEXT Best
Practices
Reference//EN) and system ID
(reference.xml) of the entity reference. At this
point, the parser checks to see if an implementation of the
EntityResolver interface has been registered with
the
setEntityResolver( ) method on an XMLReader instance. If
one has been registered, the parser invokes the
resolveEntity( ) method with the public and system IDs extracted ...