Use the InputSource Class Correctly
When
using the SAX API, all input begins with the
org.xml.sax.InputSource class. This is a class
that allows the specification of an input (e.g., a file or I/O
stream), as well as a public and system ID. SAX then extracts this
information from the InputSource at parse time and
is able to resolve external entities and other document
source-specific resources.
In fact, SAX uses the InputSource class even when
you do not. Consider the code fragment in Example 5-6, which uses JAXP to initiate a SAX parse.
import java.io.*; import java.xml.parsers.*; File myFile = ... DefaultHandler myHandler = ... SAXParserFactory spf = SAXParserFactory.newInstance( ); SAXParser parser = spf.newSAXParser( ); parser.parse(myFile, myHandler);
Even
though a java.io.File is passed in to the
SAXParser
parse( ) method,
this is converted to a SAX
InputSource
before being handed off to the underlying SAX implementation.
That’s because this JAXP code will eventually hand
off its unparsed data to the
org.xml.sax.XMLReader
class, which offers only the following two signatures for its
parse( ) method:
public void parse(InputSource inputSource); public void parse(String systemID);
You might think the second method is easier, but most SAX
implementations actually turn around and convert the string-based
system ID into an InputSource and recall the first
parse( ) method. Put succinctly, all roads lead to
the parse( ) method ...