Chapter 3. Implementing Simple APIs for XML

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Part II.  JAXP: Java API for XML Processing


Chapter 3. Implementing Simple APIs for XML

IN THIS CHAPTER

  • The Packages in JAXP for Using SAX

  • The Key JAXP Classes and Interfaces for SAX Support

  • The JAXP and Reference Implementation JAR Files

  • Creating a SAX-Parsing Application

An XML document is essentially a text document that is meaningful only when an application can process it and get the desired data. There are three standards by which an XML document can be processed: Simple APIs for XML (SAX), Document Object Model (DOM), and XSL Transformations (XSLT).

SAX is a public domain API that uses an event-driven mechanism to parse XML documents. This implies that the parser reads an XML document, and each time it recognizes a valid XML syntax (such as <, </, and so on), it notifies the application running it. The notification is done by means of callback methods.

For example, assume an XML document has the following element:

<bookname>JAX APIs</bookname> 

When the parser reaches the < symbol, it calls a method named startElement(). When the text is reached, it calls the characters() method, and for </, the parser will call the endElement() method. These and other methods are available through a number of interfaces that you implement in your application.

The DOM APIs enable an application to parse through the XML document and create an in-memory representation of the XML document. This makes it possible for applications to access and modify the data. Because it loads the entire XML document in memory, it is very processor-intensive, and therefore might have performance issues with large documents.

SAX, on the other hand, processes documents serially, and throws callbacks whenever an XML component is reached. It does not load any information in memory. This makes the processing of XML faster compared to DOM APIs. However, because it does not create an in-memory representation of XML data, it is not possible to manipulate data using SAX.

XSLT is a language description, which provides a mechanism by which an application can transform an XML document into another XML document, or other outputs such as HTML.

There are a large number of parsers, such as Xerces and Crimson, that support the SAX, DOM, and XSLT standards. However, you need to know the implementation details of these parsers to use them. Also, if you decide to change the parser implementation in your application, you need to change and recompile your code.

Java APIs for XML (JAXP) endorses the SAX, DOM, and XSLT standards, and provides a standard and consistent set of APIs with which you can process and transform XML documents. These APIs are independent of a vendor-specific parser implementation. Therefore, in your application you can use the same API calls, irrespective of whether you use Xerces, Crimson, or any other parser that follows the DOM, SAX and XSLT specifications. This is made possible by the pluggability layer that enables you to choose and use a specific parser implementation at runtime. The pluggability layer also ensures that you do not need to change the application code even if you change the parser implementation.

This chapter explains how JAXP enables you to process an XML document using a SAX-based parser. This chapter is divided into two parts. The first part explains the JAXP packages that contain the SAX packages and describes the APIs that are used by an application to process an XML document. The second part takes you through a step-by-step process to implement JAXP in your application to process an XML document using a SAX parser.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch03]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net