|
SAX parsers use a callback mechanism to parse XML. Applications register a to receive the parsing events. Although this is a little more complicated, it's more efficient because there's no need to build any data structures.Because Sun's JAXP API only supports SAX 1.0, the following example instantiates the Resin parser directly. Of course, if you want to parse HTML with a SAX API, you'll need to instantiate instead.The following example just prints the element names, properly indented, as they're parsed.
is a convenient abstract implementation of the SAX handler APIs. By extending , you can just implement the methods your application needs. The SAX parser will call when it finishes parsing the open tag. In this case, I'm ignoring the namespace junk and just using the tag name, .The callback just decrements the depth to get the correct value.
To parse with SAX, you must register your handler using .
And the result looks like:
|