|
| | When you create a parser instance, the default error handler does nothing.
This means that your program will fail silently when it encounters an error.
You should register an error handler with the parser by supplying a class
which implements the org.xml.sax.ErrorHandler
interface. This is true regardless of whether your parser is a
DOM based or SAX based parser.
|
 |  |  |  | How do I read data from a stream as it arrives? |  |  |  |  |
| | For performance reasons, all the standard Xerces processing
uses readers which buffer the input. In order to read data
from a stream as it arrives, you need to instruct Xerces to
use the StreamingCharReader class as its reader.
To do this, create a subclass of
org.apache.xerces.readers.DefaultReaderFactory
and override createCharReader and
createUTF8Reader as shown below.
 |  |  |  |
public class StreamingCharFactory extends org.apache.xerces.readers.DefaultReaderFactory {
public XMLEntityHandler.EntityReader createCharReader(XMLEntityHandler entityHandler,
XMLErrorReporter errorReporter,
boolean sendCharDataAsCharArray,
Reader reader,
StringPool stringPool) throws Exception
{
return new org.apache.xerces.readers.StreamingCharReader(entityHandler, errorReporter, sendCharDataAsCharArray, reader, stringPool);
}
public XMLEntityHandler.EntityReader createUTF8Reader(XMLEntityHandler entityHandler,
XMLErrorReporter errorReporter,
boolean sendCharDataAsCharArray,
InputStream data,
StringPool stringPool) throws Exception
{
XMLEntityHandler.EntityReader reader;
reader = new org.apache.xerces.readers.StreamingCharReader(entityHandler, errorReporter, sendCharDataAsCharArray, new InputStreamReader(data, "UTF8"), stringPool);
return reader;
}
}
|  |  |  |  |
In your program, after you instantiate a parser class, replace
the DefaultReaderFactory with StreamingCharFactory , and be
sure to wrap the InputStream that you are reading
from with an InputStreamReader .
 |  |  |  |
InputStream in = ... ;
SAXParser p = new SAXParser();
DocumentHandler h = ... ;
// set the correct reader factory
p.setReaderFactory(((StreamingSAXClient)h).new StreamingCharFactory());
p.setDocumentHandler(h);
// be sure to wrap the input stream in an InputStreamReader.
p.parse(new InputSource(new InputStreamReader(in)));
|  |  |  |  |
|
|