Menu

Expat Function Reference

September 1, 1999

Clark Cooper

Expat Functions

Parser Creation

XML_ParserCreate

XML_Parser XML_ParserCreate(const XML_Char*encoding)
  • US-ASCII
  • UTF-8
  • UTF-16
  • ISO-8859-1

XML_ParserCreateNS

XML_Parser XML_ParserCreateNS(const XML_Char*encoding, XML_Char sep)
sepsep

XML_ExternalEntityParserCreate

XML_Parser XML_ExternalEntityParserCreate(XML_Parser p, const XML_Char *context, const XML_Char *encoding)

XML_ParserFree

void XML_ParserFree(XML_Parser p)
UserData

Parsing

XML_Parse

int XML_Parse(XML_Parser p, const char *s, int len, int isFinal)
slenslensisFinallen

XML_ParseBuffer

int XML_ParseBuffer(XML_Parser p, int len, int isFinal)
XML_GetBuffer

XML_GetBuffer

void *XML_GetBuffer(XML_Parser p, int len)
lenXML_ParseBuffer

for (;;) {

  int bytes_read;

  void *buff = XML_GetBuffer(p, BUFF_SIZE);

  if (buff == NULL) {

    /* handle error */

  }



  bytes_read = read(docfd, buff, BUFF_SIZE);

  if (bytes_read < 0) {

    /* handle error */

  }



  if (! XML_ParseBuffer(p, bytes_read, bytes_read == 0)) {

    /* handle parse error */

  }



  if (bytes_read == 0)

    break;

}  


Handler Setting

Although handlers are typically set prior to parsing and left alone, an application may choose to set or change the handler for a parsing event while the parse is in progress. For instance, your application may choose to ignore all text not descended from a para element. One way it could do this is to set the character handler when a para start tag is seen, and unset it for the corresponding end tag.

A handler may be unset by providing a NULL pointer to the appropriate handler setter. None of the handler setting functions have a return value.

Your handlers will be receiving strings in arrays of type XML_Char. This type is defined in xmlparse.h and is conditional upon the setting of either of the XML_UNICODE macros. If neither of these is set, then XML_Char contains characters encoding UTF-8. Otherwise you'll be receiving UTF-16 in the form of either unsigned short or wchar_t characters.

Note that you'll receive them in this form independent of the original encoding of the document. Elsewhere in this document, I may make this point by simply referring to UTF-8.


XML_SetElementHandler


XML_SetElementHandler(XML_Parser p,

                      XML_StartElementHandler start,

                      XML_EndElementHandler end);


typedef void

(*XML_StartElementHandler)(void *userData,

                           const XML_Char *name,

                           const XML_Char **atts);


typedef void

(*XML_EndElementHandler)(void *userData,

                         const XML_Char *name);

Set handlers for start and end tags. Attributes are passed to the start handler as a pointer to a vector of char pointers. Each attribute seen in a start (or empty) tag occupies 2 consecutive places in this vector: the attribute name followed by the attribute value. These pairs are terminated by a null pointer.


XML_SetCharacterDataHandler


XML_SetCharacterDataHandler(XML_Parser p,

                            XML_CharacterDataHandler charhndl)


typedef void

(*XML_CharacterDataHandler)(void *userData,

                            const XML_Char *s,

                            int len);

Set a text handler. The string your handler receives is NOT zero terminated. You have to use the length argument to deal with the end of the string. A single block of contiguous text free of markup may still result in a sequence of calls to this handler. In other words, if you're searching for a pattern in the text, it may be split across calls to this handler.


XML_SetProcessingInstructionHandler


XML_SetProcessingInstructionHandler(XML_Parser p,

                                    XML_ProcessingInstructionHandler proc)


typedef void

(*XML_ProcessingInstructionHandler)(void *userData,

                                    const XML_Char *target,

                                    const XML_Char *data);



Set a handler for processing instructions. The target is the first word in the processing instruction. The data is the rest of the characters in it after skipping all whitespace after the initial word.


XML_SetCommentHandler


XML_SetCommentHandler (XML_Parser p,

                      XML_CommentHandler cmnt)


typedef void

(*XML_CommentHandler)(void *userData,

                      const XML_Char *data);

Set a handler for comments. The data is all text inside the comment delimiters.


XML_SetCdataSectionHandler


XML_SetCdataSectionHandler(XML_Parser p,

                           XML_StartCdataSectionHandler start,

                           XML_EndCdataSectionHandler end)


typedef void

(*XML_StartCdataSectionHandler)(void *userData);


typedef void

(*XML_EndCdataSectionHandler)(void *userData);

Sets handlers that get called at the beginning and end of a CDATA section.


XML_SetDefaultHandler


XML_SetDefaultHandler(XML_Parser p,

                      XML_DefaultHandler hndl)


typedef void

(*XML_DefaultHandler)(void *userData,

                      const XML_Char *s,

                      int len);

Sets a handler for any characters in the document which wouldn't otherwise be handled. This includes both data for which no handlers can be set (like some kinds of DTD declarations) and data which could be reported but which currently has no handler set. Note that a contiguous piece of data that is destined to be reported to the default handler may actually be reported over several calls to the handler. Setting the handler with this call has the side effect of turning off expansion of references to internally defined general entities. Instead these references are passed to the default handler.


XML_SetDefaultHandlerExpand


XML_SetDefaultHandlerExpand(XML_Parser p,

                            XML_DefaultHandler hndl)

This sets a default handler, but doesn't affect expansion of internal entity references.


XML_SetExternalEntityRefHandler


XML_SetExternalEntityRefHandler(XML_Parser p,

                                XML_ExternalEntityRefHandler hndl)


typedef int

(*XML_ExternalEntityRefHandler)(XML_Parser parser,

                                const XML_Char *context,

                                const XML_Char *base,

                                const XML_Char *systemId,

                                const XML_Char *publicId);

Set an external entity reference handler. This handler is also called for processing an external DTD subset if parameter entity parsing is in effect. (See XML_SetParamEntityParsing )

The base parameter is the base to use for relative system identifiers. It is set by XML_SetBase and may be null. The public id parameter is the public id given in the entity declaration and may be null. The system id is the system identifier specified in the entity declaration and is never null.

There are a couple of ways in which this handler differs from others. First, this handler returns an integer. A non-zero value should be returned for successful handling of the external entity reference. Returning a zero indicates failure, and causes the calling parser to return an XML_ERROR_EXTERNAL_ENTITY_HANDLING error.

Second, instead of having userData as its first argument, it receives the parser that encountered the entity reference. This, along with the context parameter, may be used as arguments to a call to XML_ExternalEntityParserCreate. Using the returned parser, the body of the external entity can be recursively parsed.

Since this handler may be called recursively, it should not be saving information into global or static variables.


XML_SetUnknownEncodingHandler


XML_SetUnknownEncodingHandler(XML_Parser p,

                              XML_UnknownEncodingHandler enchandler,

         void *encodingHandlerData)


typedef int

(*XML_UnknownEncodingHandler)(void *encodingHandlerData,

                              const XML_Char *name,

                              XML_Encoding *info);

Set a handler to deal with encodings other than the built in set. If the handler knows how to deal with an encoding with the given name, it should fill in the info data structure and return 1. Otherwise it should return 0.


typedef struct {

  int map[256];

  void *data;

  int (*convert)(void *data, const char *s);

  void (*release)(void *data);

} XML_Encoding;

The map array contains information for every possible possible leading byte in a byte sequence. If the corresponding value is >= 0, then it's a single byte sequence and the byte encodes that Unicode value. If the value is -1, then that byte is invalid as the initial byte in a sequence. If the value is -n, where n is an integer > 1, then n is the number of bytes in the sequence and the actual conversion is accomplished by a call to the function pointed at by convert. This function may return -1 if the sequence itself is invalid. The convert pointer may be null if there are only single byte encodings. The data parameter passed to the convert function is the data pointer from XML_Encoding. The string s is NOT null terminated and points at the sequence of bytes to be converted.

The function pointed at by release is called by the parser when it is finished with the encoding. It may be null.


XML_SetNamespaceDeclHandler


XML_SetNamespaceDeclHandler(XML_Parser p,

                            XML_StartNamespaceDeclHandler start,

                            XML_EndNamespaceDeclHandler end)


typedef void

(*XML_StartNamespaceDeclHandler)(void *userData,

                                 const XML_Char *prefix,

                                 const XML_Char *uri);


typedef void

(*XML_EndNamespaceDeclHandler)(void *userData,

                               const XML_Char *prefix);

Set handlers for namespace declarations. Namespace declarations occur inside start tags. But the namespace declaration start handler is called before the start tag handler for each namespace declared in that start tag. The corresponding namespace end handler is called after the end tag for the element the namespace is associated with.


XML_SetUnparsedEntityDeclHandler


XML_SetUnparsedEntityDeclHandler(XML_Parser p,

                                 XML_UnparsedEntityDeclHandler h)


typedef void

(*XML_UnparsedEntityDeclHandler)(void *userData,

                                 const XML_Char *entityName,

                                 const XML_Char *base,

                                 const XML_Char *systemId,

                                 const XML_Char *publicId,

                                 const XML_Char *notationName);

Set a handler that receives declarations of unparsed entities. These are entity declarations that have a notation (NDATA) field:


<!ENTITY logo SYSTEM "images/logo.gif" NDATA gif>

So for this example, the entityName would be "logo", the systemId would be "images/logo.gif" and notationName would be "gif". For this example the publicId parameter is null. The base parameter would be whatever has been set with XML_SetBase. If not set, it would be null.


XML_SetNotationDeclHandler


XML_SetNotationDeclHandler(XML_Parser p,

                           XML_NotationDeclHandler h)


typedef void

(*XML_NotationDeclHandler)(void *userData,

                           const XML_Char *notationName,

                           const XML_Char *base,

                           const XML_Char *systemId,

                           const XML_Char *publicId);

Set a handler that receives notation declarations.


XML_SetNotStandaloneHandler


XML_SetNotStandaloneHandler(XML_Parser p,

                            XML_NotStandaloneHandler h)


typedef int 

(*XML_NotStandaloneHandler)(void *userData);

Set a handler that is called if the document is not "standalone". This happens when there is an external subset or a reference to a parameter entity, but does not have standalone set to "yes" in an XML declaration. If this handler returns 0, then the parser will throw an XML_ERROR_NOT_STANDALONE error.


Parse position and error reporting functions

These are the functions you'll want to call when the parse functions return 0, although the position reporting functions are useful outside of errors. The position reported is that of the first of the sequence of characters that generated the current event (or the error that caused the parse functions to return 0.)


XML_GetErrorCode

enum XML_Error XML_GetErrorCode(XML_Parser p)


XML_ErrorString

const XML_LChar *XML_ErrorString(int code)


XML_GetCurrentByteIndex

long XML_GetCurrentByteIndex(XML_Parser p)


XML_GetCurrentLineNumber

int XML_GetCurrentLineNumber(XML_Parser p)


XML_GetCurrentColumnNumber

int XML_GetCurrentColumnNumber(XML_Parser p)


Miscellaneous functions

The functions in this section either obtain state information from the parser or can be used to dynamically set parser options.


XML_SetUserData

XML_SetUserData(XML_Parser p, void *userData)

XML_GetUserData

void * XML_GetUserData(XML_Parser p)

XML_UseParserAsHandlerArg

void XML_UseParserAsHandlerArg(XML_Parser p)

XML_SetBase

int XML_SetBase(XML_Parser p, const XML_Char *base)

XML_GetBase

const XML_Char * XML_GetBase(XML_Parser p)

XML_GetSpecifiedAttributeCount

int XML_GetSpecifiedAttributeCount(XML_Parser p)

XML_SetEncoding

int XML_SetEncoding(XML_Parser p, const XML_Char *encoding)

XML_SetParamEntityParsing

int XML_SetParamEntityParsing(XML_Parser p, enum XML_ParamEntityParsing code)
  • XML_PARAM_ENTITY_PARSING_NEVER
  • XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
  • XML_PARAM_ENTITY_PARSING_ALWAYS