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
DOM equivalent of a
SAXParser is the
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 ...