Home

This Chapter
-Chapter 10: Streaming API for XML
-Reading XML
-Writing to XML
-Summary

Table of Contents
-Introduction
-Chapter 1: Core Libraries
-Chapter 2: Dynamic Compilation
-Chapter 3: Scripting
-Chapter 4: Networking
-Chapter 5: Swing Updates
-Chapter 6: Abstract Window Toolkit
-Chapter 7: Internationalization
-Chapter 8: Java Database Connectivity 4.0
-Chapter 9: XML Digital Signature API
-Chapter 10: Streaming API for XML
-Chapter 11: Java Architecture for XML Binding
-Chapter 12: Web Services
-Chapter 13: JavaBeans Activation Framework
-Chapter 14: User-Defined MXBeans
-Chapter 15: Concurrency Updates
-Appendix A: Enums
-Appendix B: Generics
-Appendix C: Annotations

Previous
Next

 

Chapter 10

Streaming API for XML

Prior to the Streaming API for XML (StAX), defined in JSR 173 (http://jcp.org/en/jsr/detail?id=173), to handle XML Java programmers had to work with the two main APIs for XML processing, DOM and SAX. DOM is easy to use and provides you with the ability to convert XML into a tree of objects. The downside is, however, the fact that DOM is inefficient for large XML documents since the whole object tree must be loaded into memory. The event-based SAX is more efficient in terms of memory usage as it does not have to load the whole XML document into memory. In fact, SAX, though harder to program, is fast and can work well with any size of documents. SAX is based on the Observer design pattern, it calls the appropriate listener as it encounters various elements of an XML document. This is a push technology in action. The problem with SAX is that data pushing is not always the perfect solution with all clients. Sometimes, the client, rather than the parser, needs to control the parsing process. In addition, SAX can only be used to read, not write.

Then came StAX. It is a technology similar to SAX, except that StAX is based on a pull technology. It is the client that queries the StAX parser for more elements. It allows you to iterate over the elements being read, similar to having a ResultSet in a JDBC application.

The classes and interfaces that make up the StAX framework belong to the javax.xml.stream package and its two subpackages, javax.xml.stream.events and javax.xml.stream.util. The four most prominent interfaces are XMLStreamReader, XMLEventReader, XMLStreamWriter, and XMLEventWriter, all of which are part of javax.xml.stream. As the names imply, the first two interfaces are used for reading XML documents and the last two for creating or writing ones.

Previous
Next