“Gotcha!”

Before leaving our introduction to parsing XML documents, there are a few pitfalls to make you aware of. These “gotchas” will help you avoid common programming mistakes when using SAX, and we will discuss more of these for other APIs in the appropriate sections.

My Parser Doesn’t Support SAX 2.0: What Can I Do?

For those of you who are unlucky enough not to have a parser with SAX 2.0 support, don’t despair. First, you always have the option of changing parsers; keeping current on SAX standards is an important part of an XML parser’s responsibility, and if your vendor is not doing this, you may have other concerns to address with them as well. However, there are certainly cases where you are forced to use a parser because of legacy code or applications; in these situations, you are still not “left out in the cold.”

SAX 2.0 includes a helper class, org.xml.sax.helpers.ParserAdapter , which can actually cause a SAX 1.0 Parser implementation to behave like a SAX 2.0 XMLReader implementation. This handy class takes in a 1.0 Parser implementation as an input parameter and then can be used in the stead of that implementation. It allows a ContentHandler to be set, and handles all namespace callbacks properly. The only feature loss you will see is that skipped entities will not be reported, as this capability was not available in a 1.0 implementation in any form, and cannot be emulated by a 2.0 adapter class. The sample class would be used as shown in Example 3.6.

Example 3-6. Using ...

Get Java and XML now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.