Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

Using XML Parser for Java, 4 of 22

## DOM and SAX APIs

XML APIs generally fall into the following two categories:

- Event-based

- Tree-based

See Figure 17-3. Consider the following simple XML document:

    <?xml version="1.0"?>
      <EMPLIST>
        <EMP>
         <ENAME>MARY</ENAME>
        </EMP>
        <EMP>
         <ENAME>SCOTT</ENAME>
        </EMP>
      </EMPLIST>

### DOM: Tree-Based API

A tree-based API (such as Document Object Model, DOM) builds an in-memory tree representation of the XML document. It provides classes and methods for an application to navigate and process the tree.

In general, the DOM interface is most useful for structural manipulations of the XML tree, such as reordering elements, adding or deleting elements and attributes, renaming elements, and so on. For example, for the XML document above, the DOM creates an in-memory tree structure as shown in

### SAX: Event -Based API

An event-based API (such as SAX) uses callbacks to report parsing events to the application. The application deals with these events through customized event handlers. Events include the start and end of elements and characters.

Unlike tree-based APIs, event-based APIs usually do not build in-memory tree representations of the XML documents. Therefore, in general, SAX is useful for applications that do not need to manipulate the XML tree, such as search operations, among others.

The above XML document becomes a series of linear events as shown in Figure 17-3.

#### Figure 17-3 Comparing DOM (Tree-Based) and SAX (Event-Based) APIs

[

### Guidelines for Using DOM and SAX APIs

Here are some 'rules of thumb' for using DOM and SAX APIs:

#### DOM:

- Use the DOM interface when when you need some sort of random access.

- DOM consumes more memory.

- DOM is also good when you are trying to transformations of some sort.

- DOM is also good when you want to have tree iteration and want to walk through the entire document tree.

- When using the DOM interface, see if you can use more attributes over elements in your XML (to reduce the pipe size).

#### SAX:

Use the SAX interface when data comes in a streaming manner (using some I/Pstream).

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index