Chapter 8. Document Processing by Programming in Java

Table of Contents

8.1. Document Object Model (DOM)
8.2. Simple API for XML (SAX)
8.3. Stream API for XML (StAX)
8.4. Showing an Interactive Tree View
8.4.1. Building a JTree with DOM
8.4.2. Building a JTree with SAX
8.4.3. Building a JTree with StAX
8.5. Additional Information on Programming Models

In the previous sections we have described how to process XML files with XML declarative tools. But it is also possible to use standard programming languages to process XML files, thus allowing a much finer control on the output and a better integration with other types of processing. Processing XML files is often done in Java, but C++, C#, C, Prolog, Haskell, Perl, PHP, Ruby and Python also have packages to do so. Chapter 10 will show a few examples of handling XML files in different programming languages other than Java.

But before we explain how to program our compacting example in Java, it is important to understand different models of XML processing: Document Object Model (DOM), Simple API for XML (SAX) and Streaming API for XML (StAX).

The DOM programming model is similar to the one we have been using implicitely in previous chapters: by reading an XML file, a parser builds an internal tree structure file which it then traverses and modifies. Using a programming language without any restriction instead of a rule language such as XSLT, it is possible to destroy the tree structures with those manipulations so it is important to have a rigorous programming discipline.

Building the entire tree structure in memory before starting to process it can be prohibitive in the case of large XML files, so SAX, an alternative programming model, has been defined: as elements are parsed, user defined call-back procedures are invoked to do the processing. This requires much less memory because only a part of the document needs to be kept in memory at all time. However, the program is more limited in the kind of processing it can do efficiently or simply. This limitation is similar to the one observed between algorithms reading random-access files and those reading sequential-access files.

On top of the DOM and SAX approaches to parsing that have been available in the standard Java API since version 1.4, another streaming approach, introduced in Java 1.6, implements pull parsing that offers a relatively simple programming model and an efficient memory management. In this case, the program asks for the next XML token from the parser in order to select the appropriate action according to its type. So contrarily to the SAX approach in which the program is called back by the parser at each new token, in a pull-parser it is the program that controls the progression in the XML document. So processing of the file can be stopped as soon as it has been determined that the rest of the file is not relevant or it is possible to skip rapidly over irrelevant parts of the document for a certain application.

In this chapter, we will show how our compact pretty-print application shown in Section 5.3 can be implemented with each of the DOM, SAX and StAX programming models.

In Java, all XML document processing classes have been unified under the Java API for XML Processing (JAXP), for controlling XML parsing, validation and transformation. It describes a unified interface independently of particular XML parsers and transformation engines.