DOM

The DOM API, unlike the SAX API, allows programmers to construct an object model representing a document, and then traverse and modify that representation. The DOM API is not Java-specific; it was developed by the W3C XML working group as a cross-platform API for manipulating XML files (see http://www.w3c.org/XML/). As a result, it sometimes doesn’t take the most direct Java-based path to a particular result. The JAXP 1.1 API incorporates DOM Level 2.

DOM is useful when programs need random access to a complex XML document, or to a document whose format is not known ahead of time. This flexibility does come at a cost, however, as the parser must build a complete in-memory object representation of the document. For larger documents, the resource requirements mount quickly. Consequently, many applications use a combination of SAX and DOM, using SAX to parse longer documents (such as importing large amounts of transactional data from an Enterprise Reporting System), and using DOM to deal with smaller, more complex documents that may require alteration (such as processing configuration files or transforming existing XML documents).

Getting a DOM Parser

The DOM equivalent of a SAXParser is the org.w3c.dom.DocumentBuilder. Many DocumentBuilder implementations actually use SAX to parse the underlying document, so the DocumentBuilder implementation itself can be thought of as a layer that sits on top of SAX to provide a different view of the structure of an XML document. We use ...

Get Java Enterprise in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.