Chapter 4 XML Processing
benefit of avoiding the creation of an intermediate resource consuming represen
tation. Finally, SAX is good for implementing stream processing where the busi
ness logic is invoked in the midst of document processing. However, SAX can be
tedious to use for more complex documents that necessitate managing sophisti
cated context, and in these cases, developers may find it better to use DOM or
In summary, consider using the SAX processing model when any of the fol
lowing circumstances apply:
You are familiar with event based programming.
Your application only consumes documents without making structural modifi
cations to them.
The document must only be processed one time.
You have to effectively extract and process only parts of the document.
Memory usage is an issue or documents may potentially be very large.
You want to implement performant stream processing, such as for dealing with
very large documents.
The structure of a document and the order of its information map well to the
domain specific objects or corresponds to the order in which discrete methods
of the application's logic must be invoked. Otherwise, you may have to main
tain rather complicated contexts.
Note that the SAX model may not be the best candidate for application devel
opers who are more concerned about implementing business logic.
DOM Programming Model
With the DOM programming model, you write code to traverse a tree like data
structure created by the parser from the source document. Typically, processing the
XML input data is done in a minimum of two steps, as follows:
1. The DOM parser creates a tree like data structure that models the XML source
document. This structure is called a DOM tree.
2. The application code walks the DOM tree, searching for relevant information
that it extracts, consolidates, and processes further. Developers can use consol