SAX

At the base of nearly all Java and XML APIs is SAX, the Simple API for XML. The first part of making good decisions with SAX is deciding whether to use SAX. Generally, alpha-geek types want to use SAX and nothing else, while everyone else avoids it like the plague. The mystique of using SAX and the complexity that makes it daunting are both poor reasons to decide for or against using SAX. Better criteria are presented in the following questions:

  • Am I only reading and not writing or outputting XML?

  • Is speed my primary concern (over usability, for example)?

  • Do I need to work with only portions of the input XML?

  • Are elements and attributes in the input XML independent (no one part of the document depends on or references another part of the document)?

If you can answer “yes” to all these questions, SAX is well-suited for your application. If you cannot, you might want to think about using DOM, as detailed later in this chapter.

Get Java Enterprise Best Practices 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.