[All Packages]  [Previous]  [Next]

W3C SAX APIs

SAX is a standard interface for event-based XML parsing, developed collaboratively by the members of the XML-DEV mailing list.

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 may also be included; that context pointer will be passed to each SAX function.

The SAX callback structure:

typedef struct
{
   sword (*startDocument)(void *ctx);
   sword (*endDocument)(void *ctx);
   sword (*startElement)(void *ctx, const oratext *name, const struct xmlnodes *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);
   sword (*comment)(void *ctx, const oratext *data);
   sword (*elementDecl)(void *ctx, const oratext *name, const oratext *content);
   sword (*attributeDecl)(void *ctx, const oratext *elem,
                          const oratext *attr, const oratext *body);
   sword (*xmlDecl)(void *ctx, const oratext *version, boolean encoding);
} xmlsaxcb;


Data Structures and Types

NameDeclarationDefinition
ORATEXT* typedef unsigned char oratext; Pointer to data (any encoding, including Unicode -- cast as needed)
SWORD typedef signed int sword; Return value for user SAX callback functions; 0 means success
XMLNODES* typedef struct xmlnodes xmlnodes; Pointer to list of nodes (attributes of an element).

Note: xmlnodes is an opaque type and cannot be referenced directly. Instead, use DOM functions getAttributeIndex et al.


Callback Functions conforming to the SAX standard

characters Receive notification of character data inside an element.
endDocument Receive notification of the end of the document.
endElement Receive notification of the end of an element.
ignorableWhitespace Receive notification of ignorable whitespace in element content.
notationDecl Receive notification of a notation declaration.
processingInstruction Receive notification of a processing instruction.
startDocument Receive notification of the beginning of the document.
startElement Receive notification of the start of an element.
unparsedEntityDecl Receive notification of an unparsed entity declaration.

Non-SAX Callback Functions

attributeDecl Receive notification about an element attribute declaration in the DTD
comment Receive notification about a comment in the XML document
elementDecl Receive notification about an element declaration in the DTD
nsStartElement Receive notification of the start of a namespace for an element
xmlDecl Receive notification of an XML declaration (XMLDecl)

Function Prototypes


NAME

startDocument

PURPOSE

Receives notification of the beginning of the document.

SYNTAX

sword (*startDocument)(void *ctx);
PARAMETERS

ctx(IN)client context

NAME

endDocument

PURPOSE

Receives notification of the end of the document.

SYNTAX

sword (*endDocument)(void *ctx);
PARAMETERS

ctx(IN)client context

NAME

startElement

PURPOSE

Receives notification of the beginning of an element.

SYNTAX
sword (*startElement)(void *ctx, const oratext *name, const struct xmlnodes *attrs);
PARAMETERS

ctx(IN)client context
name(IN)element type name
attrs(IN)specified or defaulted attributes

NAME

nsStartElement

PURPOSE

Receives namespace-aware notification of the start of an element.

SYNTAX
sword (*nsStartElement)(void *ctx, const oratext *qname, const oratext *local,
                        const oratext *namespace, const struct xmlnodes *attrs);
PARAMETERS

ctx(IN)client context
qname(IN)element fully qualified name
local(IN)element local name
namespace(IN)element namespace (URI)
attrs(IN)specified or defaulted attributes


NAME

endElement

PURPOSE

Receives notification of the end of an element.

SYNTAX

sword (*endElement)(void *ctx, const oratext *name);
PARAMETERS

ctx(IN)client context
name(IN)element type name

NAME

characters

PURPOSE

Receives notification of character data inside an element.

SYNTAX

sword (*characters)(void *ctx, const oratext *ch, size_t len);
PARAMETERS

ctx(IN)client context pointer
ch(IN)pointer to character data array
len(IN)number of characters to use from the pointer

NAME

ignorableWhitespace

PURPOSE

Receives notification of ignorable whitespace in element content.

SYNTAX

sword (*ignorableWhitespace)(void *ctx, const oratext *ch, size_t len);
PARAMETERS

ctx(IN)client context
ch(IN)pointer to whitespace character array
len(IN)number of characters to use from the pointer

NAME

processingInstruction

PURPOSE

Receives notification of a processing instruction.

SYNTAX

sword (*processingInstruction)(void *ctx, const oratext *target, const oratext *data);
PARAMETERS

ctx(IN)client context
target(IN)processing instruction target
data(IN)processing instruction data, or NULL if none is supplied

NAME

comment

PURPOSE

Receives notification about an XML source comment.

SYNTAX
sword (*comment)(void *ctx, const oratext *data);
PARAMETERS

ctx(IN)client context
data(IN)body of comment


NAME

elementDecl

PURPOSE

Receives notification about an element declaration.

SYNTAX
sword (*elementDecl)(void *ctx, const oratext *name, const oratext *content);
PARAMETERS

ctx(IN)client context
name(IN)name of element being declared
content(IN)content model for element


NAME

attributeDecl

PURPOSE

Receives notification about an element's attribute declaration.

SYNTAX
sword (*attributeDecl)(void *ctx, const oratext *elem,
                          const oratext *attr, const oratext *body);
PARAMETERS

ctx(IN)client context
name(IN)name of element that attribute is being declared for
attr(IN)name of attribute being declared
body(IN)body of attribute declaration


NAME

notationDecl

PURPOSE

Receives notification of a notation declaration.

SYNTAX
sword (*notationDecl)(void *ctx, const oratext *name, const oratext *publicId,
                                                      const oratext *systemId);
PARAMETERS

ctx(IN)client context
name(IN)notation name
publicIdI(IN)notation public identifier, or NULL if not available
systemId(IN)notation system identifier

NAME

unparsedEntityDecl

PURPOSE

Receives notification of an unparsed entity declaration.

SYNTAX
sword (*unparsedEntityDecl)(void *ctx, const oratext *name, const oratext *publicId,
                            const oratext *systemId, const oratext *notationName);
PARAMETERS

ctx(IN)client context
name(IN)entity name
publicId(IN)entity public identifier, or NULL if not available
systemId(IN)entity system identifier
notationName(IN)name of the associated notation


NAME

xmlDecl

PURPOSE

Receives notification about an XML declaration (XMLDecl).

SYNTAX
sword (*xmlDecl)(void *ctx, const oratext *version, boolean encoding);
PARAMETERS

ctx(IN)client context
version(IN)version string from declaration (in data encoding)
encoding(IN)flag indicating if encoding was specified in declaration

NOTES

If an encoding was specified in the XMLDecl, encoding will be TRUE. Since the document's data will be converted to the chosen data encoding, there is no point in providing the original encoding. Instead, the data encoding can get retrieved with getEncoding(). Any new XMLDecl written out will specify the data encoding.