Chapter 3. Producing SAX2 Events

The preceding chapter provided an overview of the most widely used SAX classes and showed how they relate to each other. This chapter drills more deeply into how to produce XML events with SAX, including further customization of SAX parsers.

Pull Mode Event Production with XMLReader

Most of the time you work with SAX2, you’ll be using some kind of org.xml.sax.XMLReader implementation that turns XML text into a SAX event stream. Such a class is loosely called a “SAX parser.” Don’t confuse this with the older SAX1 org.xml.sax.Parser class. New code should not be using that class!

This interface works in a kind of “pull” mode: when a thread makes an XMLReader.parse() request, it blocks until the XML document has been fully read and processed. Inside the parser there’s a lot of work going on, including a “pull-to-push” adapter: the parser pulls data out of the input source provided to parse() and converts it to events that it pushes to event consumers. This model is different from the model of a java.io.Reader, from which applications can only get individual buffers of character data, but it’s also similar because in both cases the calling thread is pulling data from a stream.

You can also have pure “push” mode event producers. The most common kind writes events directly to event handlers and doesn’t use any kind of input abstraction to indicate the data’s source; it’s not parsing XML text. We discuss several types of such producers later ...

Get SAX2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.