Home TOC |
![]() ![]() ![]() |
Simple API for XML
In this chapter we focus on the Simple API for XML (SAX), an event-driven, serial-access mechanism for accessing XML documents. This is the protocol that most servlets and network-oriented programs will want to use to transmit and receive XML documents, because it's the fastest and least memory-intensive mechanism that is currently available for dealing with XML documents.
The SAX protocol requires a lot more programming than the Document Object Model (DOM). It's an event-driven model (you provide the callback methods, and the parser invokes them as it reads the XML data), which makes it harder to visualize. Finally, you can't "back up" to an earlier part of the document, or rearrange it, any more than you can back up a serial data stream or rearrange characters you have read from that stream.
For those reasons, developers who are writing a user-oriented application that displays an XML document and possibly modifies it will want to use the DOM mechanism described in the next part of the tutorial, Document Object Model (page 163).
However, even if you plan to do build DOM apps exclusively, there are several important reasons for familiarizing yourself with the SAX model:
- Same Error Handling
- When parsing a document for a DOM, the same kinds of exceptions are generated, so the error handling for JAXP SAX and DOM apps are identical.
- Handling Validation Errors
- By default, the specifications require that validation errors (which you'll be learning more about in this part of the tutorial) are ignored. If you want to throw an exception in the event of a validation error (and you probably do) then you need to understand how the SAX error handling works.
- Converting Existing Data
- As you'll see in the DOM section of the tutorial, Sun's reference implementation provides a mechanism you can use to convert an existing data set to XML--however, taking advantage of that mechanism requires an understanding the SAX model.
In This Chapter
- Writing a Simple XML File
- Echoing an XML File with the SAX Parser
- Adding Additional Event Handlers
- Handling Errors with the Nonvalidating Parser
- Substituting and Inserting Text
- Creating a Document Type Definition (DTD)
- DTD's Effect on the Nonvalidating Parser
- Defining Attributes and Entities in the DTD
- Referencing Binary Entities
- Using the Validating Parser
- Defining Parameter Entities and Conditional Sections
- Parsing the Parameterized DTD
- Handling Lexical Events
- Using the DTDHandler and EntityResolver
Home TOC |
![]() ![]() ![]() |