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

Using XML Parser for C++, 7 of 10

## DOM and SAX APIs

Oracle XML parser for C++ checks if an XML document is well-formed, and optionally validates it against a DTD. The parser constructs an object tree which can be accessed via one of the following interfaces:

- DOM interface

- Serially via a SAX interface

These two XML APIs:

- DOM: Tree-based APIs. A tree-based API compiles an XML document into an internal tree structure, then allows an application to navigate that tree using the Document Object Model (DOM), a standard tree-based API for XML and HTML documents.

- SAX: Event-based APIs. An event-based API, on the other hand, reports parsing events (such as the start and end of elements) directly to the application through callbacks, and does not usually build an internal tree. The application implements handlers to deal with the different events, much like handling events in a graphical user interface.

Tree-based APIs are useful for a wide range of applications, but they often put a great strain on system resources, especially if the document is large (under very controlled circumstances, it is possible to construct the tree in a lazy fashion to avoid some of this problem). Furthermore, some applications need to build their own, different data trees, and it is very inefficient to build a tree of parse nodes, only to map it onto a new tree.

In both of these cases, an event-based API provides a simpler, lower-level access to an XML document: you can parse documents much larger than your available system memory, and you can construct your own data structures using your callback event handlers.

### Using the SAX API

To use SAX, an `xmlsaxcb` structure is initialized with function pointers and passed to the `xmlinit()` call. A pointer to a user-defined context structure can also be included. That context pointer will be passed to each SAX function.

#### SAX Callback Structure

The SAX callback structure:

    typedef struct
    {
     sword (*startDocument)(void *ctx);
     sword (*endDocument)(void *ctx);
     sword (*startElement)(void *ctx, const oratext *name,
                 const struct xmlarray *attrs);
     sword (*endElement)(void *ctx, const oratext *name);
     sword (*characters)(void *ctx, const oratext *ch, size_t len);
     sword (*ignorableWhitespace)(void *ctx, const oratext *ch, size_t len);
     sword (*processingInstruction)(void *ctx, const oratext *target,
                 const oratext *data);
     sword (*notationDecl)(void *ctx, const oratext *name,
                 const oratext *publicId, const oratext *systemId);
     sword (*unparsedEntityDecl)(void *ctx, const oratext *name,
                 const oratext *publicId,
                 const oratext *systemId, const oratext *notationName);
     sword (*nsStartElement)(void *ctx, const oratext *qname,
                 const oratext *local, const oratext *nsp,
                 const struct xmlnodes *attrs);
    } xmlsaxcb;

### Using the DOM API

See "XML Parser for C++ Example 6: C++ -- DOMSample.cpp" .

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