1 <?xml version=
"1.0" encoding=
"iso-8859-1"?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
6 <!-- Copyright 1999,2000 Clark Cooper <coopercc@netheaven.com>
8 This is free software. You may distribute or modify according to
9 the terms of the MIT/X License -->
10 <title>Expat XML Parser
</title>
11 <meta name=
"author" content=
"Clark Cooper, coopercc@netheaven.com" />
12 <meta http-equiv=
"Content-Style-Type" content=
"text/css" />
13 <link href=
"style.css" rel=
"stylesheet" type=
"text/css" />
16 <table cellspacing=
"0" cellpadding=
"0" width=
"100%">
18 <td class=
"corner"><img src=
"expat.png" alt=
"(Expat logo)" /></td>
19 <td class=
"banner"><h1>The Expat XML Parser
</h1></td>
22 <td class=
"releaseno">Release
1.95.8</td>
28 <p>Expat is a library, written in C, for parsing XML documents. It's
29 the underlying XML parser for the open source Mozilla project, Perl's
30 <code>XML::Parser
</code>, Python's
<code>xml.parsers.expat
</code>, and
31 other open-source XML parsers.
</p>
33 <p>This library is the creation of James Clark, who's also given us
34 groff (an nroff look-alike), Jade (an implemention of ISO's DSSSL
35 stylesheet language for SGML), XP (a Java XML parser package), XT (a
36 Java XSL engine). James was also the technical lead on the XML
37 Working Group at W3C that produced the XML specification.
</p>
39 <p>This is free software, licensed under the
<a
40 href=
"../COPYING">MIT/X Consortium license
</a>. You may download it
41 from
<a href=
"http://www.libexpat.org/">the Expat home page
</a>.
44 <p>The bulk of this document was originally commissioned as an article
45 by
<a href=
"http://www.xml.com/">XML.com
</a>. They graciously allowed
46 Clark Cooper to retain copyright and to distribute it with Expat.
47 This version has been substantially extended to include documentation
48 on features which have been added since the original article was
49 published, and additional information on using the original
53 <h2>Table of Contents
</h2>
55 <li><a href=
"#overview">Overview
</a></li>
56 <li><a href=
"#building">Building and Installing
</a></li>
57 <li><a href=
"#using">Using Expat
</a></li>
58 <li><a href=
"#reference">Reference
</a>
60 <li><a href=
"#creation">Parser Creation Functions
</a>
62 <li><a href=
"#XML_ParserCreate">XML_ParserCreate
</a></li>
63 <li><a href=
"#XML_ParserCreateNS">XML_ParserCreateNS
</a></li>
64 <li><a href=
"#XML_ParserCreate_MM">XML_ParserCreate_MM
</a></li>
65 <li><a href=
"#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate
</a></li>
66 <li><a href=
"#XML_ParserFree">XML_ParserFree
</a></li>
67 <li><a href=
"#XML_ParserReset">XML_ParserReset
</a></li>
70 <li><a href=
"#parsing">Parsing Functions
</a>
72 <li><a href=
"#XML_Parse">XML_Parse
</a></li>
73 <li><a href=
"#XML_ParseBuffer">XML_ParseBuffer
</a></li>
74 <li><a href=
"#XML_GetBuffer">XML_GetBuffer
</a></li>
75 <li><a href=
"#XML_StopParser">XML_StopParser
</a></li>
76 <li><a href=
"#XML_ResumeParser">XML_ResumeParser
</a></li>
77 <li><a href=
"#XML_GetParsingStatus">XML_GetParsingStatus
</a></li>
80 <li><a href=
"#setting">Handler Setting Functions
</a>
82 <li><a href=
"#XML_SetStartElementHandler">XML_SetStartElementHandler
</a></li>
83 <li><a href=
"#XML_SetEndElementHandler">XML_SetEndElementHandler
</a></li>
84 <li><a href=
"#XML_SetElementHandler">XML_SetElementHandler
</a></li>
85 <li><a href=
"#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler
</a></li>
86 <li><a href=
"#XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler
</a></li>
87 <li><a href=
"#XML_SetCommentHandler">XML_SetCommentHandler
</a></li>
88 <li><a href=
"#XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler
</a></li>
89 <li><a href=
"#XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler
</a></li>
90 <li><a href=
"#XML_SetCdataSectionHandler">XML_SetCdataSectionHandler
</a></li>
91 <li><a href=
"#XML_SetDefaultHandler">XML_SetDefaultHandler
</a></li>
92 <li><a href=
"#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand
</a></li>
93 <li><a href=
"#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler
</a></li>
94 <li><a href=
"#XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg
</a></li>
95 <li><a href=
"#XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler
</a></li>
96 <li><a href=
"#XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler
</a></li>
97 <li><a href=
"#XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler
</a></li>
98 <li><a href=
"#XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler
</a></li>
99 <li><a href=
"#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler
</a></li>
100 <li><a href=
"#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler
</a></li>
101 <li><a href=
"#XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler
</a></li>
102 <li><a href=
"#XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler
</a></li>
103 <li><a href=
"#XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler
</a></li>
104 <li><a href=
"#XML_SetElementDeclHandler">XML_SetElementDeclHandler
</a></li>
105 <li><a href=
"#XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler
</a></li>
106 <li><a href=
"#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler
</a></li>
107 <li><a href=
"#XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler
</a></li>
108 <li><a href=
"#XML_SetNotationDeclHandler">XML_SetNotationDeclHandler
</a></li>
109 <li><a href=
"#XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler
</a></li>
112 <li><a href=
"#position">Parse Position and Error Reporting Functions
</a>
114 <li><a href=
"#XML_GetErrorCode">XML_GetErrorCode
</a></li>
115 <li><a href=
"#XML_ErrorString">XML_ErrorString
</a></li>
116 <li><a href=
"#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex
</a></li>
117 <li><a href=
"#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber
</a></li>
118 <li><a href=
"#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber
</a></li>
119 <li><a href=
"#XML_GetCurrentByteCount">XML_GetCurrentByteCount
</a></li>
120 <li><a href=
"#XML_GetInputContext">XML_GetInputContext
</a></li>
123 <li><a href=
"#miscellaneous">Miscellaneous Functions
</a>
125 <li><a href=
"#XML_SetUserData">XML_SetUserData
</a></li>
126 <li><a href=
"#XML_GetUserData">XML_GetUserData
</a></li>
127 <li><a href=
"#XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg
</a></li>
128 <li><a href=
"#XML_SetBase">XML_SetBase
</a></li>
129 <li><a href=
"#XML_GetBase">XML_GetBase
</a></li>
130 <li><a href=
"#XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount
</a></li>
131 <li><a href=
"#XML_GetIdAttributeIndex">XML_GetIdAttributeIndex
</a></li>
132 <li><a href=
"#XML_SetEncoding">XML_SetEncoding
</a></li>
133 <li><a href=
"#XML_SetParamEntityParsing">XML_SetParamEntityParsing
</a></li>
134 <li><a href=
"#XML_UseForeignDTD">XML_UseForeignDTD
</a></li>
135 <li><a href=
"#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet
</a></li>
136 <li><a href=
"#XML_DefaultCurrent">XML_DefaultCurrent
</a></li>
137 <li><a href=
"#XML_ExpatVersion">XML_ExpatVersion
</a></li>
138 <li><a href=
"#XML_ExpatVersionInfo">XML_ExpatVersionInfo
</a></li>
139 <li><a href=
"#XML_GetFeatureList">XML_GetFeatureList
</a></li>
140 <li><a href=
"#XML_FreeContentModel">XML_FreeContentModel
</a></li>
141 <li><a href=
"#XML_MemMalloc">XML_MemMalloc
</a></li>
142 <li><a href=
"#XML_MemRealloc">XML_MemRealloc
</a></li>
143 <li><a href=
"#XML_MemFree">XML_MemFree
</a></li>
151 <h2><a name=
"overview">Overview
</a></h2>
153 <p>Expat is a stream-oriented parser. You register callback (or
154 handler) functions with the parser and then start feeding it the
155 document. As the parser recognizes parts of the document, it will
156 call the appropriate handler for that part (if you've registered one.)
157 The document is fed to the parser in pieces, so you can start parsing
158 before you have all the document. This also allows you to parse really
159 huge documents that won't fit into memory.
</p>
161 <p>Expat can be intimidating due to the many kinds of handlers and
162 options you can set. But you only need to learn four functions in
163 order to do
90% of what you'll want to do with it:
</p>
167 <dt><code><a href=
"#XML_ParserCreate"
168 >XML_ParserCreate
</a></code></dt>
169 <dd>Create a new parser object.
</dd>
171 <dt><code><a href=
"#XML_SetElementHandler"
172 >XML_SetElementHandler
</a></code></dt>
173 <dd>Set handlers for start and end tags.
</dd>
175 <dt><code><a href=
"#XML_SetCharacterDataHandler"
176 >XML_SetCharacterDataHandler
</a></code></dt>
177 <dd>Set handler for text.
</dd>
179 <dt><code><a href=
"#XML_Parse"
180 >XML_Parse
</a></code></dt>
181 <dd>Pass a buffer full of document to the parser
</dd>
184 <p>These functions and others are described in the
<a
185 href=
"#reference">reference
</a> part of this document. The reference
186 section also describes in detail the parameters passed to the
187 different types of handlers.
</p>
189 <p>Let's look at a very simple example program that only uses
3 of the
190 above functions (it doesn't need to set a character handler.) The
191 program
<a href=
"../examples/outline.c">outline.c
</a> prints an
192 element outline, indenting child elements to distinguish them from the
193 parent element that contains them. The start handler does all the
194 work. It prints two indenting spaces for every level of ancestor
195 elements, then it prints the element and attribute
196 information. Finally it increments the global
<code>Depth
</code>
203 start(void *data, const char *el, const char **attr) {
206 for (i =
0; i
< Depth; i++)
211 for (i =
0; attr[i]; i +=
2) {
212 printf(
" %s='%s'", attr[i], attr[i +
1]);
217 } /* End of start handler */
220 <p>The end tag simply does the bookkeeping work of decrementing
221 <code>Depth
</code>.
</p>
224 end(void *data, const char *el) {
226 } /* End of end handler */
229 <p>Note the
<code>XMLCALL
</code> annotation used for the callbacks.
230 This is used to ensure that the Expat and the callbacks are using the
231 same calling convention in case the compiler options used for Expat
232 itself and the client code are different. Expat tries not to care
233 what the default calling convention is, though it may require that it
234 be compiled with a default convention of
"cdecl" on some platforms.
235 For code which uses Expat, however, the calling convention is
236 specified by the
<code>XMLCALL
</code> annotation on most platforms;
237 callbacks should be defined using this annotation.
</p>
239 <p>The
<code>XMLCALL
</code> annotation was added in Expat
1.95.7, but
240 existing working Expat applications don't need to add it (since they
241 are already using the
"cdecl" calling convention, or they wouldn't be
242 working). The annotation is only needed if the default calling
243 convention may be something other than
"cdecl". To use the annotation
244 safely with older versions of Expat, you can conditionally define it
245 <em>after
</em> including Expat's header file:
</p>
248 #include
<expat.h
>
251 #if defined(_MSC_EXTENSIONS)
&& !defined(__BEOS__)
&& !defined(__CYGWIN__)
252 #define XMLCALL __cdecl
253 #elif defined(__GNUC__)
254 #define XMLCALL __attribute__((cdecl))
261 <p>After creating the parser, the main program just has the job of
262 shoveling the document to the parser so that it can do its work.
</p>
265 <h2><a name=
"building">Building and Installing Expat
</a></h2>
267 <p>The Expat distribution comes as a compressed (with GNU gzip) tar
268 file. You may download the latest version from
<a href=
269 "http://sourceforge.net/projects/expat/" >Source Forge
</a>. After
270 unpacking this, cd into the directory. Then follow either the Win32
271 directions or Unix directions below.
</p>
273 <h3>Building under Win32
</h3>
275 <p>If you're using the GNU compiler under cygwin, follow the Unix
276 directions in the next section. Otherwise if you have Microsoft's
277 Developer Studio installed, then from Windows Explorer double-click on
278 "expat.dsp" in the lib directory and build and install in the usual
281 <p>Alternatively, you may download the Win32 binary package that
282 contains the
"expat.h" include file and a pre-built DLL.
</p>
284 <h3>Building under Unix (or GNU)
</h3>
286 <p>First you'll need to run the configure shell script in order to
287 configure the Makefiles and headers for your system.
</p>
289 <p>If you're happy with all the defaults that configure picks for you,
290 and you have permission on your system to install into /usr/local, you
291 can install Expat with this sequence of commands:
</p>
299 <p>There are some options that you can provide to this script, but the
300 only one we'll mention here is the
<code>--prefix
</code> option. You
301 can find out all the options available by running configure with just
302 the
<code>--help
</code> option.
</p>
304 <p>By default, the configure script sets things up so that the library
305 gets installed in
<code>/usr/local/lib
</code> and the associated
306 header file in
<code>/usr/local/include
</code>. But if you were to
307 give the option,
<code>--prefix=/home/me/mystuff
</code>, then the
308 library and header would get installed in
309 <code>/home/me/mystuff/lib
</code> and
310 <code>/home/me/mystuff/include
</code> respectively.
</p>
312 <h3>Configuring Expat Using the Pre-Processor
</h3>
314 <p>Expat's feature set can be configured using a small number of
315 pre-processor definitions. The definition of this symbols does not
316 affect the set of entry points for Expat, only the behavior of the API
317 and the definition of character types in the case of
318 <code>XML_UNICODE_WCHAR_T
</code>. The symbols are:
</p>
320 <dl class=
"cpp-symbols">
322 <dd>Include support for using and reporting DTD-based content. If
323 this is defined, default attribute values from an external DTD subset
324 are reported and attribute value normalization occurs based on the
325 type of attributes defined in the external subset. Without
326 this, Expat has a smaller memory footprint and can be faster, but will
327 not load external entities or process conditional sections. This does
328 not affect the set of functions available in the API.
</dd>
331 <dd>When defined, support for the
<cite><a href=
332 "http://www.w3.org/TR/REC-xml-names/" >Namespaces in XML
</a></cite>
333 specification is included.
</dd>
336 <dd>When defined, character data reported to the application is
337 encoded in UTF-
16 using wide characters of the type
338 <code>XML_Char
</code>. This is implied if
339 <code>XML_UNICODE_WCHAR_T
</code> is defined.
</dd>
341 <dt>XML_UNICODE_WCHAR_T
</dt>
342 <dd>If defined, causes the
<code>XML_Char
</code> character type to be
343 defined using the
<code>wchar_t
</code> type; otherwise,
<code>unsigned
344 short
</code> is used. Defining this implies
345 <code>XML_UNICODE
</code>.
</dd>
347 <dt>XML_CONTEXT_BYTES
</dt>
348 <dd>The number of input bytes of markup context which the parser will
349 ensure are available for reporting via
<code><a href=
350 "#XML_GetInputContext" >XML_GetInputContext
</a></code>. This is
351 normally set to
1024, and must be set to a positive interger. If this
352 is not defined, the input context will not be available and
<code><a
353 href=
"#XML_GetInputContext" >XML_GetInputContext
</a></code> will
354 always report NULL. Without this, Expat has a smaller memory
355 footprint and can be faster.
</dd>
358 <dd>On Windows, this should be set if Expat is going to be linked
359 statically with the code that calls it; this is required to get all
360 the right MSVC magic annotations correct. This is ignored on other
365 <h2><a name=
"using">Using Expat
</a></h2>
367 <h3>Compiling and Linking Against Expat
</h3>
369 <p>Unless you installed Expat in a location not expected by your
370 compiler and linker, all you have to do to use Expat in your programs
371 is to include the Expat header (
<code>#include
<expat.h
></code>)
372 in your files that make calls to it and to tell the linker that it
373 needs to link against the Expat library. On Unix systems, this would
374 usually be done with the
<code>-lexpat
</code> argument. Otherwise,
375 you'll need to tell the compiler where to look for the Expat header
376 and the linker where to find the Expat library. You may also need to
377 take steps to tell the operating system where to find this libary at
380 <p>On a Unix-based system, here's what a Makefile might look like when
381 Expat is installed in a standard location:
</p>
388 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)
391 <p>If you installed Expat in, say,
<code>/home/me/mystuff
</code>, then
392 the Makefile would look like this:
</p>
396 CFLAGS= -I/home/me/mystuff/include
398 LIBS= -L/home/me/mystuff/lib -lexpat
400 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)
403 <p>You'd also have to set the environment variable
404 <code>LD_LIBRARY_PATH
</code> to
<code>/home/me/mystuff/lib
</code> (or
405 to
<code>${LD_LIBRARY_PATH}:/home/me/mystuff/lib
</code> if
406 LD_LIBRARY_PATH already has some directories in it) in order to run
407 your application.
</p>
409 <h3>Expat Basics
</h3>
411 <p>As we saw in the example in the overview, the first step in parsing
412 an XML document with Expat is to create a parser object. There are
<a
413 href=
"#creation">three functions
</a> in the Expat API for creating a
414 parser object. However, only two of these (
<code><a href=
415 "#XML_ParserCreate" >XML_ParserCreate
</a></code> and
<code><a href=
416 "#XML_ParserCreateNS" >XML_ParserCreateNS
</a></code>) can be used for
417 constructing a parser for a top-level document. The object returned
418 by these functions is an opaque pointer (i.e.
"expat.h" declares it as
419 void *) to data with further internal structure. In order to free the
420 memory associated with this object you must call
<code><a href=
421 "#XML_ParserFree" >XML_ParserFree
</a></code>. Note that if you have
422 provided any
<a href=
"#userdata">user data
</a> that gets stored in the
423 parser, then your application is responsible for freeing it prior to
424 calling
<code>XML_ParserFree
</code>.
</p>
426 <p>The objects returned by the parser creation functions are good for
427 parsing only one XML document or external parsed entity. If your
428 application needs to parse many XML documents, then it needs to create
429 a parser object for each one. The best way to deal with this is to
430 create a higher level object that contains all the default
431 initialization you want for your parser objects.
</p>
433 <p>Walking through a document hierarchy with a stream oriented parser
434 will require a good stack mechanism in order to keep track of current
435 context. For instance, to answer the simple question,
"What element
436 does this text belong to?" requires a stack, since the parser may have
437 descended into other elements that are children of the current one and
438 has encountered this text on the way out.
</p>
440 <p>The things you're likely to want to keep on a stack are the
441 currently opened element and it's attributes. You push this
442 information onto the stack in the start handler and you pop it off in
445 <p>For some tasks, it is sufficient to just keep information on what
446 the depth of the stack is (or would be if you had one.) The outline
447 program shown above presents one example. Another such task would be
448 skipping over a complete element. When you see the start tag for the
449 element you want to skip, you set a skip flag and record the depth at
450 which the element started. When the end tag handler encounters the
451 same depth, the skipped element has ended and the flag may be
452 cleared. If you follow the convention that the root element starts at
453 1, then you can use the same variable for skip flag and skip
458 init_info(Parseinfo *info) {
461 /* Other initializations here */
462 } /* End of init_info */
465 rawstart(void *data, const char *el, const char **attr) {
466 Parseinfo *inf = (Parseinfo *) data;
469 if (should_skip(inf, el, attr)) {
470 inf-
>skip = inf-
>depth;
473 start(inf, el, attr); /* This does rest of start handling */
477 } /* End of rawstart */
480 rawend(void *data, const char *el) {
481 Parseinfo *inf = (Parseinfo *) data;
486 end(inf, el); /* This does rest of end handling */
488 if (inf-
>skip == inf-
>depth)
493 <p>Notice in the above example the difference in how depth is
494 manipulated in the start and end handlers. The end tag handler should
495 be the mirror image of the start tag handler. This is necessary to
496 properly model containment. Since, in the start tag handler, we
497 incremented depth
<em>after
</em> the main body of start tag code, then
498 in the end handler, we need to manipulate it
<em>before
</em> the main
499 body. If we'd decided to increment it first thing in the start
500 handler, then we'd have had to decrement it last thing in the end
503 <h3 id=
"userdata">Communicating between handlers
</h3>
505 <p>In order to be able to pass information between different handlers
506 without using globals, you'll need to define a data structure to hold
507 the shared variables. You can then tell Expat (with the
<code><a href=
508 "#XML_SetUserData" >XML_SetUserData
</a></code> function) to pass a
509 pointer to this structure to the handlers. This is the first
510 argument received by most handlers. In the
<a href=
"#reference"
511 >reference section
</a>, an argument to a callback function is named
512 <code>userData
</code> and have type
<code>void *
</code> if the user
513 data is passed; it will have the type
<code>XML_Parser
</code> if the
514 parser itself is passed. When the parser is passed, the user data may
515 be retrieved using
<code><a href=
"#XML_GetUserData"
516 >XML_GetUserData
</a></code>.
</p>
518 <p>One common case where multiple calls to a single handler may need
519 to communicate using an application data structure is the case when
520 content passed to the character data handler (set by
<code><a href=
521 "#XML_SetCharacterDataHandler"
522 >XML_SetCharacterDataHandler
</a></code>) needs to be accumulated. A
523 common first-time mistake with any of the event-oriented interfaces to
524 an XML parser is to expect all the text contained in an element to be
525 reported by a single call to the character data handler. Expat, like
526 many other XML parsers, reports such data as a sequence of calls;
527 there's no way to know when the end of the sequence is reached until a
528 different callback is made. A buffer referenced by the user data
529 structure proves both an effective and convenient place to accumulate
532 <!-- XXX example needed here -->
537 <p>Expat is an XML
1.0 parser, and as such never complains based on
538 the value of the
<code>version
</code> pseudo-attribute in the XML
539 declaration, if present.
</p>
541 <p>If an application needs to check the version number (to support
542 alternate processing), it should use the
<code><a href=
543 "#XML_SetXmlDeclHandler" >XML_SetXmlDeclHandler
</a></code> function to
544 set a handler that uses the information in the XML declaration to
545 determine what to do. This example shows how to check that only a
546 version number of
<code>"1.0"</code> is accepted:
</p>
549 static int wrong_version;
550 static XML_Parser parser;
553 xmldecl_handler(void *userData,
554 const XML_Char *version,
555 const XML_Char *encoding,
558 static const XML_Char Version_1_0[] = {'
1', '.', '
0',
0};
562 for (i =
0; i
< (sizeof(Version_1_0) / sizeof(Version_1_0[
0])); ++i) {
563 if (version[i] != Version_1_0[i]) {
565 /* also clear all other handlers: */
566 XML_SetCharacterDataHandler(parser, NULL);
575 <h3>Namespace Processing
</h3>
577 <p>When the parser is created using the
<code><a href=
578 "#XML_ParserCreateNS" >XML_ParserCreateNS
</a></code>, function, Expat
579 performs namespace processing. Under namespace processing, Expat
580 consumes
<code>xmlns
</code> and
<code>xmlns:...
</code> attributes,
581 which declare namespaces for the scope of the element in which they
582 occur. This means that your start handler will not see these
583 attributes. Your application can still be informed of these
584 declarations by setting namespace declaration handlers with
<a href=
585 "#XML_SetNamespaceDeclHandler"
586 ><code>XML_SetNamespaceDeclHandler
</code></a>.
</p>
588 <p>Element type and attribute names that belong to a given namespace
589 are passed to the appropriate handler in expanded form. By default
590 this expanded form is a concatenation of the namespace URI, the
591 separator character (which is the
2nd argument to
<code><a href=
592 "#XML_ParserCreateNS" >XML_ParserCreateNS
</a></code>), and the local
593 name (i.e. the part after the colon). Names with undeclared prefixes
594 are passed through to the handlers unchanged, with the prefix and
595 colon still attached. Unprefixed attribute names are never expanded,
596 and unprefixed element names are only expanded when they are in the
597 scope of a default namespace.
</p>
599 <p>However if
<code><a href=
"#XML_SetReturnNSTriplet"
600 >XML_SetReturnNSTriplet
</a></code> has been called with a non-zero
601 <code>do_nst
</code> parameter, then the expanded form for names with
602 an explicit prefix is a concatenation of: URI, separator, local name,
603 separator, prefix.
</p>
605 <p>You can set handlers for the start of a namespace declaration and
606 for the end of a scope of a declaration with the
<code><a href=
607 "#XML_SetNamespaceDeclHandler" >XML_SetNamespaceDeclHandler
</a></code>
608 function. The StartNamespaceDeclHandler is called prior to the start
609 tag handler and the EndNamespaceDeclHandler is called before the
610 corresponding end tag that ends the namespace's scope. The namespace
611 start handler gets passed the prefix and URI for the namespace. For a
612 default namespace declaration (xmlns='...'), the prefix will be null.
613 The URI will be null for the case where the default namespace is being
614 unset. The namespace end handler just gets the prefix for the closing
617 <p>These handlers are called for each declaration. So if, for
618 instance, a start tag had three namespace declarations, then the
619 StartNamespaceDeclHandler would be called three times before the start
620 tag handler is called, once for each declaration.
</p>
622 <h3>Character Encodings
</h3>
624 <p>While XML is based on Unicode, and every XML processor is required
625 to recognized UTF-
8 and UTF-
16 (
1 and
2 byte encodings of Unicode),
626 other encodings may be declared in XML documents or entities. For the
627 main document, an XML declaration may contain an encoding
630 <?xml
version=
"1.0" encoding=
"ISO-8859-2"?
>
633 <p>External parsed entities may begin with a text declaration, which
634 looks like an XML declaration with just an encoding declaration:
</p>
636 <?xml
encoding=
"Big5"?
>
639 <p>With Expat, you may also specify an encoding at the time of
640 creating a parser. This is useful when the encoding information may
641 come from a source outside the document itself (like a higher level
644 <p><a name=
"builtin_encodings"></a>There are four built-in encodings
653 <p>Anything else discovered in an encoding declaration or in the
654 protocol encoding specified in the parser constructor, triggers a call
655 to the
<code>UnknownEncodingHandler
</code>. This handler gets passed
656 the encoding name and a pointer to an
<code>XML_Encoding
</code> data
657 structure. Your handler must fill in this structure and return
658 <code>XML_STATUS_OK
</code> if it knows how to deal with the
659 encoding. Otherwise the handler should return
660 <code>XML_STATUS_ERROR
</code>. The handler also gets passed a pointer
661 to an optional application data structure that you may indicate when
662 you set the handler.
</p>
664 <p>Expat places restrictions on character encodings that it can
665 support by filling in the
<code>XML_Encoding
</code> structure.
668 <li>Every ASCII character that can appear in a well-formed XML document
669 must be represented by a single byte, and that byte must correspond to
670 it's ASCII encoding (except for the characters $@\^'{}~)
</li>
671 <li>Characters must be encoded in
4 bytes or less.
</li>
672 <li>All characters encoded must have Unicode scalar values less than or
673 equal to
65535 (
0xFFFF)
<em>This does not apply to the built-in support
674 for UTF-
16 and UTF-
8</em></li>
675 <li>No character may be encoded by more that one distinct sequence of
679 <p><code>XML_Encoding
</code> contains an array of integers that
680 correspond to the
1st byte of an encoding sequence. If the value in
681 the array for a byte is zero or positive, then the byte is a single
682 byte encoding that encodes the Unicode scalar value contained in the
683 array. A -
1 in this array indicates a malformed byte. If the value is
684 -
2, -
3, or -
4, then the byte is the beginning of a
2,
3, or
4 byte
685 sequence respectively. Multi-byte sequences are sent to the convert
686 function pointed at in the
<code>XML_Encoding
</code> structure. This
687 function should return the Unicode scalar value for the sequence or -
1
688 if the sequence is malformed.
</p>
690 <p>One pitfall that novice Expat users are likely to fall into is that
691 although Expat may accept input in various encodings, the strings that
692 it passes to the handlers are always encoded in UTF-
8 or UTF-
16
693 (depending on how Expat was compiled). Your application is responsible
694 for any translation of these strings into other encodings.
</p>
696 <h3>Handling External Entity References
</h3>
698 <p>Expat does not read or parse external entities directly. Note that
699 any external DTD is a special case of an external entity. If you've
700 set no
<code>ExternalEntityRefHandler
</code>, then external entity
701 references are silently ignored. Otherwise, it calls your handler with
702 the information needed to read and parse the external entity.
</p>
704 <p>Your handler isn't actually responsible for parsing the entity, but
705 it is responsible for creating a subsidiary parser with
<code><a href=
706 "#XML_ExternalEntityParserCreate"
707 >XML_ExternalEntityParserCreate
</a></code> that will do the job. This
708 returns an instance of
<code>XML_Parser
</code> that has handlers and
709 other data structures initialized from the parent parser. You may then
710 use
<code><a href=
"#XML_Parse" >XML_Parse
</a></code> or
<code><a
711 href=
"#XML_ParseBuffer">XML_ParseBuffer
</a></code> calls against this
712 parser. Since external entities my refer to other external entities,
713 your handler should be prepared to be called recursively.
</p>
715 <h3>Parsing DTDs
</h3>
717 <p>In order to parse parameter entities, before starting the parse,
718 you must call
<code><a href=
"#XML_SetParamEntityParsing"
719 >XML_SetParamEntityParsing
</a></code> with one of the following
722 <dt><code>XML_PARAM_ENTITY_PARSING_NEVER
</code></dt>
723 <dd>Don't parse parameter entities or the external subset
</dd>
724 <dt><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
</code></dt>
725 <dd>Parse parameter entites and the external subset unless
726 <code>standalone
</code> was set to
"yes" in the XML declaration.
</dd>
727 <dt><code>XML_PARAM_ENTITY_PARSING_ALWAYS
</code></dt>
728 <dd>Always parse parameter entities and the external subset
</dd>
731 <p>In order to read an external DTD, you also have to set an external
732 entity reference handler as described above.
</p>
734 <h3 id=
"stop-resume">Temporarily Stopping Parsing
</h3>
736 <p>Expat
1.95.8 introduces a new feature: its now possible to stop
737 parsing temporarily from within a handler function, even if more data
738 has already been passed into the parser. Applications for this
742 <li>Supporting the
<a href=
"http://www.w3.org/TR/xinclude/"
743 >XInclude
</a> specification.
</li>
745 <li>Delaying further processing until additional information is
746 available from some other source.
</li>
748 <li>Adjusting processor load as task priorities shift within an
751 <li>Stopping parsing completely (simply free or reset the parser
752 instead of resuming in the outer parsing loop). This can be useful
753 if a application-domain error is found in the XML being parsed or if
754 the result of the parse is determined not to be useful after
758 <p>To take advantage of this feature, the main parsing loop of an
759 application needs to support this specifically. It cannot be
760 supported with a parsing loop compatible with Expat
1.95.7 or
761 earlier (though existing loops will continue to work without
762 supporting the stop/resume feature).
</p>
764 <p>An application that uses this feature for a single parser will have
765 the rough structure (in pseudo-code):
</p>
767 <pre class=
"pseudocode">
771 if parse_xml(p, fd) {
778 if ready_to_resume() {
779 suspended = continue_parsing(p, fd);
785 <p>An application that may resume any of several parsers based on
786 input (either from the XML being parsed or some other source) will
787 certainly have more interesting control structures.
</p>
789 <p>This C function could be used for the
<code>parse_xml
</code>
790 function mentioned in the pseudo-code above:
</p>
793 #define BUFF_SIZE
10240
795 /* Parse a document from the open file descriptor 'fd' until the parse
796 is complete (the document has been completely parsed, or there's
797 been an error), or the parse is stopped. Return non-zero when
798 the parse is merely suspended.
801 parse_xml(XML_Parser p, int fd)
806 enum XML_Status status;
808 void *buff = XML_GetBuffer(p, BUFF_SIZE);
810 /* handle error... */
813 bytes_read = read(fd, buff, BUFF_SIZE);
814 if (bytes_read
< 0) {
815 /* handle error... */
818 status = XML_ParseBuffer(p, bytes_read, bytes_read ==
0);
820 case XML_STATUS_ERROR:
821 /* handle error... */
823 case XML_STATUS_SUSPENDED:
832 <p>The corresponding
<code>continue_parsing
</code> function is
833 somewhat simpler, since it only need deal with the return code from
834 <code><a href=
"#XML_ResumeParser">XML_ResumeParser
</a></code>; it can
835 delegate the input handling to the
<code>parse_xml
</code>
839 /* Continue parsing a document which had been suspended. The 'p' and
840 'fd' arguments are the same as passed to parse_xml(). Return
841 non-zero when the parse is suspended.
844 continue_parsing(XML_Parser p, int fd)
846 enum XML_Status status = XML_ResumeParser(p);
848 case XML_STATUS_ERROR:
849 /* handle error... */
851 case XML_ERROR_NOT_SUSPENDED:
852 /* handle error... */
854 case XML_STATUS_SUSPENDED:
857 return parse_xml(p, fd);
861 <p>Now that we've seen what a mess the top-level parsing loop can
862 become, what have we gained? Very simply, we can now use the
<code><a
863 href=
"#XML_StopParser" >XML_StopParser
</a></code> function to stop
864 parsing, without having to go to great lengths to avoid additional
865 processing that we're expecting to ignore. As a bonus, we get to stop
866 parsing
<em>temporarily
</em>, and come back to it when we're
869 <p>To stop parsing from a handler function, use the
<code><a href=
870 "#XML_StopParser" >XML_StopParser
</a></code> function. This function
871 takes two arguments; the parser being stopped and a flag indicating
872 whether the parse can be resumed in the future.
</p>
874 <!-- XXX really need more here -->
878 <!-- ================================================================ -->
880 <h2><a name=
"reference">Expat Reference
</a></h2>
882 <h3><a name=
"creation">Parser Creation
</a></h3>
884 <pre class=
"fcndec" id=
"XML_ParserCreate">
886 XML_ParserCreate(const XML_Char *encoding);
889 Construct a new parser. If encoding is non-null, it specifies a
890 character encoding to use for the document. This overrides the document
891 encoding declaration. There are four built-in encodings:
898 Any other value will invoke a call to the UnknownEncodingHandler.
901 <pre class=
"fcndec" id=
"XML_ParserCreateNS">
903 XML_ParserCreateNS(const XML_Char *encoding,
907 Constructs a new parser that has namespace processing in effect. Namespace
908 expanded element names and attribute names are returned as a concatenation
909 of the namespace URI,
<em>sep
</em>, and the local part of the name. This
910 means that you should pick a character for
<em>sep
</em> that can't be
911 part of a legal URI.
</div>
913 <pre class=
"fcndec" id=
"XML_ParserCreate_MM">
915 XML_ParserCreate_MM(const XML_Char *encoding,
916 const XML_Memory_Handling_Suite *ms,
917 const XML_Char *sep);
919 <pre class=
"signature">
921 void *(XMLCALL *malloc_fcn)(size_t size);
922 void *(XMLCALL *realloc_fcn)(void *ptr, size_t size);
923 void (XMLCALL *free_fcn)(void *ptr);
924 } XML_Memory_Handling_Suite;
927 <p>Construct a new parser using the suite of memory handling functions
928 specified in
<code>ms
</code>. If
<code>ms
</code> is NULL, then use the
929 standard set of memory management functions. If
<code>sep
</code> is
930 non NULL, then namespace processing is enabled in the created parser
931 and the character pointed at by sep is used as the separator between
932 the namespace URI and the local part of the name.
</p>
935 <pre class=
"fcndec" id=
"XML_ExternalEntityParserCreate">
937 XML_ExternalEntityParserCreate(XML_Parser p,
938 const XML_Char *context,
939 const XML_Char *encoding);
942 Construct a new
<code>XML_Parser
</code> object for parsing an external
943 general entity. Context is the context argument passed in a call to a
944 ExternalEntityRefHandler. Other state information such as handlers,
945 user data, namespace processing is inherited from the parser passed as
946 the
1st argument. So you shouldn't need to call any of the behavior
947 changing functions on this parser (unless you want it to act
948 differently than the parent parser).
951 <pre class=
"fcndec" id=
"XML_ParserFree">
953 XML_ParserFree(XML_Parser p);
956 Free memory used by the parser. Your application is responsible for
957 freeing any memory associated with
<a href=
"#userdata">user data
</a>.
960 <pre class=
"fcndec" id=
"XML_ParserReset">
962 XML_ParserReset(XML_Parser p,
963 const XML_Char *encoding);
966 Clean up the memory structures maintained by the parser so that it may
967 be used again. After this has been called,
<code>parser
</code> is
968 ready to start parsing a new document. All handlers are cleared from
969 the parser, except for the unknownEncodingHandler. The parser's external
970 state is re-initialized except for the values of ns and ns_triplets.
971 This function may not be used on a parser created using
<code><a href=
972 "#XML_ExternalEntityParserCreate" >XML_ExternalEntityParserCreate
</a
973 ></code>; it will return
<code>XML_FALSE
</code> in that case. Returns
974 <code>XML_TRUE
</code> on success. Your application is responsible for
975 dealing with any memory associated with
<a href=
"#userdata">user data
</a>.
978 <h3><a name=
"parsing">Parsing
</a></h3>
980 <p>To state the obvious: the three parsing functions
<code><a href=
981 "#XML_Parse" >XML_Parse
</a></code>,
<code><a href=
"#XML_ParseBuffer">
982 XML_ParseBuffer
</a></code> and
<code><a href=
"#XML_GetBuffer">
983 XML_GetBuffer
</a></code> must not be
984 called from within a handler unless they operate on a separate parser
985 instance, that is, one that did not call the handler. For example, it
986 is OK to call the parsing functions from within an
987 <code>XML_ExternalEntityRefHandler
</code>, if they apply to the parser
988 created by
<code><a href=
"#XML_ExternalEntityParserCreate"
989 >XML_ExternalEntityParserCreate
</a></code>.
</p>
991 <pre class=
"fcndec" id=
"XML_Parse">
992 enum XML_Status XMLCALL
993 XML_Parse(XML_Parser p,
998 <pre class=
"signature">
1000 XML_STATUS_ERROR =
0,
1004 <div class=
"fcndef">
1005 Parse some more of the document. The string
<code>s
</code> is a buffer
1006 containing part (or perhaps all) of the document. The number of bytes of s
1007 that are part of the document is indicated by
<code>len
</code>. This means
1008 that
<code>s
</code> doesn't have to be null terminated. It also means that
1009 if
<code>len
</code> is larger than the number of bytes in the block of
1010 memory that
<code>s
</code> points at, then a memory fault is likely. The
1011 <code>isFinal
</code> parameter informs the parser that this is the last
1012 piece of the document. Frequently, the last piece is empty (i.e.
1013 <code>len
</code> is zero.)
1014 If a parse error occurred, it returns
<code>XML_STATUS_ERROR
</code>.
1015 Otherwise it returns
<code>XML_STATUS_OK
</code> value.
1018 <pre class=
"fcndec" id=
"XML_ParseBuffer">
1019 enum XML_Status XMLCALL
1020 XML_ParseBuffer(XML_Parser p,
1024 <div class=
"fcndef">
1025 This is just like
<code><a href=
"#XML_Parse" >XML_Parse
</a></code>,
1026 except in this case Expat provides the buffer. By obtaining the
1027 buffer from Expat with the
<code><a href=
"#XML_GetBuffer"
1028 >XML_GetBuffer
</a></code> function, the application can avoid double
1029 copying of the input.
1032 <pre class=
"fcndec" id=
"XML_GetBuffer">
1034 XML_GetBuffer(XML_Parser p,
1037 <div class=
"fcndef">
1038 Obtain a buffer of size
<code>len
</code> to read a piece of the document
1039 into. A NULL value is returned if Expat can't allocate enough memory for
1040 this buffer. This has to be called prior to every call to
1041 <code><a href=
"#XML_ParseBuffer" >XML_ParseBuffer
</a></code>. A
1042 typical use would look like this:
1047 void *buff = XML_GetBuffer(p, BUFF_SIZE);
1052 bytes_read = read(docfd, buff, BUFF_SIZE);
1053 if (bytes_read
< 0) {
1057 if (! XML_ParseBuffer(p, bytes_read, bytes_read ==
0)) {
1058 /* handle parse error */
1061 if (bytes_read ==
0)
1067 <pre class=
"fcndec" id=
"XML_StopParser">
1068 enum XML_Status XMLCALL
1069 XML_StopParser(XML_Parser p,
1070 XML_Bool resumable);
1072 <div class=
"fcndef">
1074 <p>Stops parsing, causing
<code><a href=
"#XML_Parse"
1075 >XML_Parse
</a></code> or
<code><a href=
"#XML_ParseBuffer"
1076 >XML_ParseBuffer
</a></code> to return. Must be called from within a
1077 call-back handler, except when aborting (when
<code>resumable
</code>
1078 is
<code>XML_FALSE
</code>) an already suspended parser. Some
1079 call-backs may still follow because they would otherwise get
1082 <li> the end element handler for empty elements when stopped in the
1083 start element handler,
</li>
1084 <li> end namespace declaration handler when stopped in the end
1085 element handler,
</li>
1087 and possibly others.
</p>
1089 <p>This can be called from most handlers, including DTD related
1090 call-backs, except when parsing an external parameter entity and
1091 <code>resumable
</code> is
<code>XML_TRUE
</code>. Returns
1092 <code>XML_STATUS_OK
</code> when successful,
1093 <code>XML_STATUS_ERROR
</code> otherwise. The possible error codes
1096 <dt><code>XML_ERROR_SUSPENDED
</code></dt>
1097 <dd>when suspending an already suspended parser.
</dd>
1098 <dt><code>XML_ERROR_FINISHED
</code></dt>
1099 <dd>when the parser has already finished.
</dd>
1100 <dt><code>XML_ERROR_SUSPEND_PE
</code></dt>
1101 <dd>when suspending while parsing an external PE.
</dd>
1104 <p>Since the stop/resume feature requires application support in the
1105 outer parsing loop, it is an error to call this function for a parser
1106 not being handled appropriately; see
<a href=
"#stop-resume"
1107 >Temporarily Stopping Parsing
</a> for more information.
</p>
1109 <p>When
<code>resumable
</code> is
<code>XML_TRUE
</code> then parsing
1110 is
<em>suspended
</em>, that is,
<code><a href=
"#XML_Parse"
1111 >XML_Parse
</a></code> and
<code><a href=
"#XML_ParseBuffer"
1112 >XML_ParseBuffer
</a></code> return
<code>XML_STATUS_SUSPENDED
</code>.
1113 Otherwise, parsing is
<em>aborted
</em>, that is,
<code><a href=
1114 "#XML_Parse" >XML_Parse
</a></code> and
<code><a href=
1115 "#XML_ParseBuffer" >XML_ParseBuffer
</a></code> return
1116 <code>XML_STATUS_ERROR
</code> with error code
1117 <code>XML_ERROR_ABORTED
</code>.
</p>
1119 <p><strong>Note:
</strong>
1120 This will be applied to the current parser instance only, that is, if
1121 there is a parent parser then it will continue parsing when the
1122 external entity reference handler returns. It is up to the
1123 implementation of that handler to call
<code><a href=
1124 "#XML_StopParser" >XML_StopParser
</a></code> on the parent parser
1125 (recursively), if one wants to stop parsing altogether.
</p>
1127 <p>When suspended, parsing can be resumed by calling
<code><a href=
1128 "#XML_ResumeParser" >XML_ResumeParser
</a></code>.
</p>
1130 <p>New in Expat
1.95.8.
</p>
1133 <pre class=
"fcndec" id=
"XML_ResumeParser">
1134 enum XML_Status XMLCALL
1135 XML_ResumeParser(XML_Parser p);
1137 <div class=
"fcndef">
1138 <p>Resumes parsing after it has been suspended with
<code><a href=
1139 "#XML_StopParser" >XML_StopParser
</a></code>. Must not be called from
1140 within a handler call-back. Returns same status codes as
<code><a
1141 href=
"#XML_Parse">XML_Parse
</a></code> or
<code><a href=
1142 "#XML_ParseBuffer" >XML_ParseBuffer
</a></code>. An additional error
1143 code,
<code>XML_ERROR_NOT_SUSPENDED
</code>, will be returned if the
1144 parser was not currently suspended.
</p>
1146 <p><strong>Note:
</strong>
1147 This must be called on the most deeply nested child parser instance
1148 first, and on its parent parser only after the child parser has
1149 finished, to be applied recursively until the document entity's parser
1150 is restarted. That is, the parent parser will not resume by itself
1151 and it is up to the application to call
<code><a href=
1152 "#XML_ResumeParser" >XML_ResumeParser
</a></code> on it at the
1153 appropriate moment.
</p>
1155 <p>New in Expat
1.95.8.
</p>
1158 <pre class=
"fcndec" id=
"XML_GetParsingStatus">
1160 XML_GetParsingStatus(XML_Parser p,
1161 XML_ParsingStatus *status);
1163 <pre class=
"signature">
1172 enum XML_Parsing parsing;
1173 XML_Bool finalBuffer;
1174 } XML_ParsingStatus;
1176 <div class=
"fcndef">
1177 <p>Returns status of parser with respect to being initialized,
1178 parsing, finished, or suspended, and whether the final buffer is being
1179 processed. The
<code>status
</code> parameter
<em>must not
</em> be
1182 <p>New in Expat
1.95.8.
</p>
1186 <h3><a name=
"setting">Handler Setting
</a></h3>
1188 <p>Although handlers are typically set prior to parsing and left alone, an
1189 application may choose to set or change the handler for a parsing event
1190 while the parse is in progress. For instance, your application may choose
1191 to ignore all text not descended from a
<code>para
</code> element. One
1192 way it could do this is to set the character handler when a para start tag
1193 is seen, and unset it for the corresponding end tag.
</p>
1195 <p>A handler may be
<em>unset
</em> by providing a NULL pointer to the
1196 appropriate handler setter. None of the handler setting functions have
1199 <p>Your handlers will be receiving strings in arrays of type
1200 <code>XML_Char
</code>. This type is defined in expat.h as
<code>char
1201 *
</code> and contains bytes encoding UTF-
8. Note that you'll receive
1202 them in this form independent of the original encoding of the
1205 <div class=
"handler">
1206 <pre class=
"setter" id=
"XML_SetStartElementHandler">
1208 XML_SetStartElementHandler(XML_Parser p,
1209 XML_StartElementHandler start);
1211 <pre class=
"signature">
1213 (XMLCALL *XML_StartElementHandler)(void *userData,
1214 const XML_Char *name,
1215 const XML_Char **atts);
1217 <p>Set handler for start (and empty) tags. Attributes are passed to the start
1218 handler as a pointer to a vector of char pointers. Each attribute seen in
1219 a start (or empty) tag occupies
2 consecutive places in this vector: the
1220 attribute name followed by the attribute value. These pairs are terminated
1221 by a null pointer.
</p>
1222 <p>Note that an empty tag generates a call to both start and end handlers
1223 (in that order).
</p>
1226 <div class=
"handler">
1227 <pre class=
"setter" id=
"XML_SetEndElementHandler">
1229 XML_SetEndElementHandler(XML_Parser p,
1230 XML_EndElementHandler);
1232 <pre class=
"signature">
1234 (XMLCALL *XML_EndElementHandler)(void *userData,
1235 const XML_Char *name);
1237 <p>Set handler for end (and empty) tags. As noted above, an empty tag
1238 generates a call to both start and end handlers.
</p>
1241 <div class=
"handler">
1242 <pre class=
"setter" id=
"XML_SetElementHandler">
1244 XML_SetElementHandler(XML_Parser p,
1245 XML_StartElementHandler start,
1246 XML_EndElementHandler end);
1248 <p>Set handlers for start and end tags with one call.
</p>
1251 <div class=
"handler">
1252 <pre class=
"setter" id=
"XML_SetCharacterDataHandler">
1254 XML_SetCharacterDataHandler(XML_Parser p,
1255 XML_CharacterDataHandler charhndl)
1257 <pre class=
"signature">
1259 (XMLCALL *XML_CharacterDataHandler)(void *userData,
1263 <p>Set a text handler. The string your handler receives
1264 is
<em>NOT nul-terminated
</em>. You have to use the length argument
1265 to deal with the end of the string. A single block of contiguous text
1266 free of markup may still result in a sequence of calls to this handler.
1267 In other words, if you're searching for a pattern in the text, it may
1268 be split across calls to this handler.
</p>
1271 <div class=
"handler">
1272 <pre class=
"setter" id=
"XML_SetProcessingInstructionHandler">
1274 XML_SetProcessingInstructionHandler(XML_Parser p,
1275 XML_ProcessingInstructionHandler proc)
1277 <pre class=
"signature">
1279 (XMLCALL *XML_ProcessingInstructionHandler)(void *userData,
1280 const XML_Char *target,
1281 const XML_Char *data);
1284 <p>Set a handler for processing instructions. The target is the first word
1285 in the processing instruction. The data is the rest of the characters in
1286 it after skipping all whitespace after the initial word.
</p>
1289 <div class=
"handler">
1290 <pre class=
"setter" id=
"XML_SetCommentHandler">
1292 XML_SetCommentHandler(XML_Parser p,
1293 XML_CommentHandler cmnt)
1295 <pre class=
"signature">
1297 (XMLCALL *XML_CommentHandler)(void *userData,
1298 const XML_Char *data);
1300 <p>Set a handler for comments. The data is all text inside the comment
1304 <div class=
"handler">
1305 <pre class=
"setter" id=
"XML_SetStartCdataSectionHandler">
1307 XML_SetStartCdataSectionHandler(XML_Parser p,
1308 XML_StartCdataSectionHandler start);
1310 <pre class=
"signature">
1312 (XMLCALL *XML_StartCdataSectionHandler)(void *userData);
1314 <p>Set a handler that gets called at the beginning of a CDATA section.
</p>
1317 <div class=
"handler">
1318 <pre class=
"setter" id=
"XML_SetEndCdataSectionHandler">
1320 XML_SetEndCdataSectionHandler(XML_Parser p,
1321 XML_EndCdataSectionHandler end);
1323 <pre class=
"signature">
1325 (XMLCALL *XML_EndCdataSectionHandler)(void *userData);
1327 <p>Set a handler that gets called at the end of a CDATA section.
</p>
1330 <div class=
"handler">
1331 <pre class=
"setter" id=
"XML_SetCdataSectionHandler">
1333 XML_SetCdataSectionHandler(XML_Parser p,
1334 XML_StartCdataSectionHandler start,
1335 XML_EndCdataSectionHandler end)
1337 <p>Sets both CDATA section handlers with one call.
</p>
1340 <div class=
"handler">
1341 <pre class=
"setter" id=
"XML_SetDefaultHandler">
1343 XML_SetDefaultHandler(XML_Parser p,
1344 XML_DefaultHandler hndl)
1346 <pre class=
"signature">
1348 (XMLCALL *XML_DefaultHandler)(void *userData,
1353 <p>Sets a handler for any characters in the document which wouldn't
1354 otherwise be handled. This includes both data for which no handlers
1355 can be set (like some kinds of DTD declarations) and data which could
1356 be reported but which currently has no handler set. The characters
1357 are passed exactly as they were present in the XML document except
1358 that they will be encoded in UTF-
8 or UTF-
16. Line boundaries are not
1359 normalized. Note that a byte order mark character is not passed to the
1360 default handler. There are no guarantees about how characters are
1361 divided between calls to the default handler: for example, a comment
1362 might be split between multiple calls. Setting the handler with
1363 this call has the side effect of turning off expansion of references
1364 to internally defined general entities. Instead these references are
1365 passed to the default handler.
</p>
1367 <p>See also
<code><a
1368 href=
"#XML_DefaultCurrent">XML_DefaultCurrent
</a></code>.
</p>
1371 <div class=
"handler">
1372 <pre class=
"setter" id=
"XML_SetDefaultHandlerExpand">
1374 XML_SetDefaultHandlerExpand(XML_Parser p,
1375 XML_DefaultHandler hndl)
1377 <pre class=
"signature">
1379 (XMLCALL *XML_DefaultHandler)(void *userData,
1383 <p>This sets a default handler, but doesn't inhibit the expansion of
1384 internal entity references. The entity reference will not be passed
1385 to the default handler.
</p>
1387 <p>See also
<code><a
1388 href=
"#XML_DefaultCurrent">XML_DefaultCurrent
</a></code>.
</p>
1391 <div class=
"handler">
1392 <pre class=
"setter" id=
"XML_SetExternalEntityRefHandler">
1394 XML_SetExternalEntityRefHandler(XML_Parser p,
1395 XML_ExternalEntityRefHandler hndl)
1397 <pre class=
"signature">
1399 (XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser p,
1400 const XML_Char *context,
1401 const XML_Char *base,
1402 const XML_Char *systemId,
1403 const XML_Char *publicId);
1405 <p>Set an external entity reference handler. This handler is also
1406 called for processing an external DTD subset if parameter entity parsing
1407 is in effect. (See
<a href=
"#XML_SetParamEntityParsing">
1408 <code>XML_SetParamEntityParsing
</code></a>.)
</p>
1410 <p>The
<code>context
</code> parameter specifies the parsing context in
1411 the format expected by the
<code>context
</code> argument to
<code><a
1412 href=
"#XML_ExternalEntityParserCreate"
1413 >XML_ExternalEntityParserCreate
</a></code>.
<code>code
</code> is
1414 valid only until the handler returns, so if the referenced entity is
1415 to be parsed later, it must be copied.
<code>context
</code> is NULL
1416 only when the entity is a parameter entity, which is how one can
1417 differentiate between general and parameter entities.
</p>
1419 <p>The
<code>base
</code> parameter is the base to use for relative
1420 system identifiers. It is set by
<code><a
1421 href=
"#XML_SetBase">XML_SetBase
</a></code> and may be NULL. The
1422 <code>publicId
</code> parameter is the public id given in the entity
1423 declaration and may be NULL.
<code>systemId
</code> is the system
1424 identifier specified in the entity declaration and is never NULL.
</p>
1426 <p>There are a couple of ways in which this handler differs from
1427 others. First, this handler returns a status indicator (an
1428 integer).
<code>XML_STATUS_OK
</code> should be returned for successful
1429 handling of the external entity reference. Returning
1430 <code>XML_STATUS_ERROR
</code> indicates failure, and causes the
1431 calling parser to return an
1432 <code>XML_ERROR_EXTERNAL_ENTITY_HANDLING
</code> error.
</p>
1434 <p>Second, instead of having the user data as its first argument, it
1435 receives the parser that encountered the entity reference. This, along
1436 with the context parameter, may be used as arguments to a call to
1437 <code><a href=
"#XML_ExternalEntityParserCreate"
1438 >XML_ExternalEntityParserCreate
</a></code>. Using the returned
1439 parser, the body of the external entity can be recursively parsed.
</p>
1441 <p>Since this handler may be called recursively, it should not be saving
1442 information into global or static variables.
</p>
1445 <pre class=
"fcndec" id=
"XML_SetExternalEntityRefHandlerArg">
1447 XML_SetExternalEntityRefHandlerArg(XML_Parser p,
1450 <div class=
"fcndef">
1451 <p>Set the argument passed to the ExternalEntityRefHandler. If
1452 <code>arg
</code> is not NULL, it is the new value passed to the
1453 handler set using
<code><a href=
"#XML_SetExternalEntityRefHandler"
1454 >XML_SetExternalEntityRefHandler
</a></code>; if
<code>arg
</code> is
1455 NULL, the argument passed to the handler function will be the parser
1458 <p><strong>Note:
</strong>
1459 The type of
<code>arg
</code> and the type of the first argument to the
1460 ExternalEntityRefHandler do not match. This function takes a
1461 <code>void *
</code> to be passed to the handler, while the handler
1462 accepts an
<code>XML_Parser
</code>. This is a historical accident,
1463 but will not be corrected before Expat
2.0 (at the earliest) to avoid
1464 causing compiler warnings for code that's known to work with this
1465 API. It is the responsibility of the application code to know the
1466 actual type of the argument passed to the handler and to manage it
1470 <div class=
"handler">
1471 <pre class=
"setter" id=
"XML_SetSkippedEntityHandler">
1473 XML_SetSkippedEntityHandler(XML_Parser p,
1474 XML_SkippedEntityHandler handler)
1476 <pre class=
"signature">
1478 (XMLCALL *XML_SkippedEntityHandler)(void *userData,
1479 const XML_Char *entityName,
1480 int is_parameter_entity);
1482 <p>Set a skipped entity handler. This is called in two situations:
</p>
1484 <li>An entity reference is encountered for which no declaration
1485 has been read
<em>and
</em> this is not an error.
</li>
1486 <li>An internal entity reference is read, but not expanded, because
1487 <a href=
"#XML_SetDefaultHandler"><code>XML_SetDefaultHandler
</code></a>
1488 has been called.
</li>
1490 <p>The
<code>is_parameter_entity
</code> argument will be non-zero for
1491 a parameter entity and zero for a general entity.
</p> <p>Note: skipped
1492 parameter entities in declarations and skipped general entities in
1493 attribute values cannot be reported, because the event would be out of
1494 sync with the reporting of the declarations or attribute values
</p>
1497 <div class=
"handler">
1498 <pre class=
"setter" id=
"XML_SetUnknownEncodingHandler">
1500 XML_SetUnknownEncodingHandler(XML_Parser p,
1501 XML_UnknownEncodingHandler enchandler,
1502 void *encodingHandlerData)
1504 <pre class=
"signature">
1506 (XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData,
1507 const XML_Char *name,
1508 XML_Encoding *info);
1513 int (XMLCALL *convert)(void *data, const char *s);
1514 void (XMLCALL *release)(void *data);
1517 <p>Set a handler to deal with encodings other than the
<a
1518 href=
"#builtin_encodings">built in set
</a>. This should be done before
1519 <code><a href=
"#XML_Parse" >XML_Parse
</a></code> or
<code><a href=
1520 "#XML_ParseBuffer" >XML_ParseBuffer
</a></code> have been called on the
1521 given parser.
</p> <p>If the handler knows how to deal with an encoding
1522 with the given name, it should fill in the
<code>info
</code> data
1523 structure and return
<code>XML_STATUS_OK
</code>. Otherwise it
1524 should return
<code>XML_STATUS_ERROR
</code>. The handler will be called
1525 at most once per parsed (external) entity. The optional application
1526 data pointer
<code>encodingHandlerData
</code> will be passed back to
1529 <p>The map array contains information for every possible possible leading
1530 byte in a byte sequence. If the corresponding value is
>=
0, then it's
1531 a single byte sequence and the byte encodes that Unicode value. If the
1532 value is -
1, then that byte is invalid as the initial byte in a sequence.
1533 If the value is -n, where n is an integer
> 1, then n is the number of
1534 bytes in the sequence and the actual conversion is accomplished by a
1535 call to the function pointed at by convert. This function may return -
1
1536 if the sequence itself is invalid. The convert pointer may be null if
1537 there are only single byte codes. The data parameter passed to the convert
1538 function is the data pointer from
<code>XML_Encoding
</code>. The
1539 string s is
<em>NOT
</em> nul-terminated and points at the sequence of
1540 bytes to be converted.
</p>
1542 <p>The function pointed at by
<code>release
</code> is called by the
1543 parser when it is finished with the encoding. It may be NULL.
</p>
1546 <div class=
"handler">
1547 <pre class=
"setter" id=
"XML_SetStartNamespaceDeclHandler">
1549 XML_SetStartNamespaceDeclHandler(XML_Parser p,
1550 XML_StartNamespaceDeclHandler start);
1552 <pre class=
"signature">
1554 (XMLCALL *XML_StartNamespaceDeclHandler)(void *userData,
1555 const XML_Char *prefix,
1556 const XML_Char *uri);
1558 <p>Set a handler to be called when a namespace is declared. Namespace
1559 declarations occur inside start tags. But the namespace declaration start
1560 handler is called before the start tag handler for each namespace declared
1561 in that start tag.
</p>
1564 <div class=
"handler">
1565 <pre class=
"setter" id=
"XML_SetEndNamespaceDeclHandler">
1567 XML_SetEndNamespaceDeclHandler(XML_Parser p,
1568 XML_EndNamespaceDeclHandler end);
1570 <pre class=
"signature">
1572 (XMLCALL *XML_EndNamespaceDeclHandler)(void *userData,
1573 const XML_Char *prefix);
1575 <p>Set a handler to be called when leaving the scope of a namespace
1576 declaration. This will be called, for each namespace declaration,
1577 after the handler for the end tag of the element in which the
1578 namespace was declared.
</p>
1581 <div class=
"handler">
1582 <pre class=
"setter" id=
"XML_SetNamespaceDeclHandler">
1584 XML_SetNamespaceDeclHandler(XML_Parser p,
1585 XML_StartNamespaceDeclHandler start,
1586 XML_EndNamespaceDeclHandler end)
1588 <p>Sets both namespace declaration handlers with a single call.
</p>
1591 <div class=
"handler">
1592 <pre class=
"setter" id=
"XML_SetXmlDeclHandler">
1594 XML_SetXmlDeclHandler(XML_Parser p,
1595 XML_XmlDeclHandler xmldecl);
1597 <pre class=
"signature">
1599 (XMLCALL *XML_XmlDeclHandler)(void *userData,
1600 const XML_Char *version,
1601 const XML_Char *encoding,
1604 <p>Sets a handler that is called for XML declarations and also for
1605 text declarations discovered in external entities. The way to
1606 distinguish is that the
<code>version
</code> parameter will be NULL
1607 for text declarations. The
<code>encoding
</code> parameter may be NULL
1608 for an XML declaration. The
<code>standalone
</code> argument will
1609 contain -
1,
0, or
1 indicating respectively that there was no
1610 standalone parameter in the declaration, that it was given as no, or
1611 that it was given as yes.
</p>
1614 <div class=
"handler">
1615 <pre class=
"setter" id=
"XML_SetStartDoctypeDeclHandler">
1617 XML_SetStartDoctypeDeclHandler(XML_Parser p,
1618 XML_StartDoctypeDeclHandler start);
1620 <pre class=
"signature">
1622 (XMLCALL *XML_StartDoctypeDeclHandler)(void *userData,
1623 const XML_Char *doctypeName,
1624 const XML_Char *sysid,
1625 const XML_Char *pubid,
1626 int has_internal_subset);
1628 <p>Set a handler that is called at the start of a DOCTYPE declaration,
1629 before any external or internal subset is parsed. Both
<code>sysid
</code>
1630 and
<code>pubid
</code> may be NULL. The
<code>has_internal_subset
</code>
1631 will be non-zero if the DOCTYPE declaration has an internal subset.
</p>
1634 <div class=
"handler">
1635 <pre class=
"setter" id=
"XML_SetEndDoctypeDeclHandler">
1637 XML_SetEndDoctypeDeclHandler(XML_Parser p,
1638 XML_EndDoctypeDeclHandler end);
1640 <pre class=
"signature">
1642 (XMLCALL *XML_EndDoctypeDeclHandler)(void *userData);
1644 <p>Set a handler that is called at the end of a DOCTYPE declaration,
1645 after parsing any external subset.
</p>
1648 <div class=
"handler">
1649 <pre class=
"setter" id=
"XML_SetDoctypeDeclHandler">
1651 XML_SetDoctypeDeclHandler(XML_Parser p,
1652 XML_StartDoctypeDeclHandler start,
1653 XML_EndDoctypeDeclHandler end);
1655 <p>Set both doctype handlers with one call.
</p>
1658 <div class=
"handler">
1659 <pre class=
"setter" id=
"XML_SetElementDeclHandler">
1661 XML_SetElementDeclHandler(XML_Parser p,
1662 XML_ElementDeclHandler eldecl);
1664 <pre class=
"signature">
1666 (XMLCALL *XML_ElementDeclHandler)(void *userData,
1667 const XML_Char *name,
1668 XML_Content *model);
1670 <pre class=
"signature">
1671 enum XML_Content_Type {
1672 XML_CTYPE_EMPTY =
1,
1680 enum XML_Content_Quant {
1687 typedef struct XML_cp XML_Content;
1690 enum XML_Content_Type type;
1691 enum XML_Content_Quant quant;
1692 const XML_Char * name;
1693 unsigned int numchildren;
1694 XML_Content * children;
1697 <p>Sets a handler for element declarations in a DTD. The handler gets
1698 called with the name of the element in the declaration and a pointer
1699 to a structure that contains the element model. It is the
1700 application's responsibility to free this data structure using
1701 <code><a href=
"#XML_FreeContentModel"
1702 >XML_FreeContentModel
</a></code>.
</p>
1704 <p>The
<code>model
</code> argument is the root of a tree of
1705 <code>XML_Content
</code> nodes. If
<code>type
</code> equals
1706 <code>XML_CTYPE_EMPTY
</code> or
<code>XML_CTYPE_ANY
</code>, then
1707 <code>quant
</code> will be
<code>XML_CQUANT_NONE
</code>, and the other
1708 fields will be zero or NULL. If
<code>type
</code> is
1709 <code>XML_CTYPE_MIXED
</code>, then
<code>quant
</code> will be
1710 <code>XML_CQUANT_NONE
</code> or
<code>XML_CQUANT_REP
</code> and
1711 <code>numchildren
</code> will contain the number of elements that are
1712 allowed to be mixed in and
<code>children
</code> points to an array of
1713 <code>XML_Content
</code> structures that will all have type
1714 XML_CTYPE_NAME with no quantification. Only the root node can be type
1715 <code>XML_CTYPE_EMPTY
</code>,
<code>XML_CTYPE_ANY
</code>, or
1716 <code>XML_CTYPE_MIXED
</code>.
</p>
1718 <p>For type
<code>XML_CTYPE_NAME
</code>, the
<code>name
</code> field
1719 points to the name and the
<code>numchildren
</code> and
1720 <code>children
</code> fields will be zero and NULL. The
1721 <code>quant
</code> field will indicate any quantifiers placed on the
1724 <p>Types
<code>XML_CTYPE_CHOICE
</code> and
<code>XML_CTYPE_SEQ
</code>
1725 indicate a choice or sequence respectively. The
1726 <code>numchildren
</code> field indicates how many nodes in the choice
1727 or sequence and
<code>children
</code> points to the nodes.
</p>
1730 <div class=
"handler">
1731 <pre class=
"setter" id=
"XML_SetAttlistDeclHandler">
1733 XML_SetAttlistDeclHandler(XML_Parser p,
1734 XML_AttlistDeclHandler attdecl);
1736 <pre class=
"signature">
1738 (XMLCALL *XML_AttlistDeclHandler)(void *userData,
1739 const XML_Char *elname,
1740 const XML_Char *attname,
1741 const XML_Char *att_type,
1742 const XML_Char *dflt,
1745 <p>Set a handler for attlist declarations in the DTD. This handler is
1746 called for
<em>each
</em> attribute. So a single attlist declaration
1747 with multiple attributes declared will generate multiple calls to this
1748 handler. The
<code>elname
</code> parameter returns the name of the
1749 element for which the attribute is being declared. The attribute name
1750 is in the
<code>attname
</code> parameter. The attribute type is in the
1751 <code>att_type
</code> parameter. It is the string representing the
1752 type in the declaration with whitespace removed.
</p>
1754 <p>The
<code>dflt
</code> parameter holds the default value. It will be
1755 NULL in the case of
"#IMPLIED" or
"#REQUIRED" attributes. You can
1756 distinguish these two cases by checking the
<code>isrequired
</code>
1757 parameter, which will be true in the case of
"#REQUIRED" attributes.
1758 Attributes which are
"#FIXED" will have also have a true
1759 <code>isrequired
</code>, but they will have the non-NULL fixed value
1760 in the
<code>dflt
</code> parameter.
</p>
1763 <div class=
"handler">
1764 <pre class=
"setter" id=
"XML_SetEntityDeclHandler">
1766 XML_SetEntityDeclHandler(XML_Parser p,
1767 XML_EntityDeclHandler handler);
1769 <pre class=
"signature">
1771 (XMLCALL *XML_EntityDeclHandler)(void *userData,
1772 const XML_Char *entityName,
1773 int is_parameter_entity,
1774 const XML_Char *value,
1776 const XML_Char *base,
1777 const XML_Char *systemId,
1778 const XML_Char *publicId,
1779 const XML_Char *notationName);
1781 <p>Sets a handler that will be called for all entity declarations.
1782 The
<code>is_parameter_entity
</code> argument will be non-zero in the
1783 case of parameter entities and zero otherwise.
</p>
1785 <p>For internal entities (
<code><!ENTITY foo
"bar"></code>),
1786 <code>value
</code> will be non-NULL and
<code>systemId
</code>,
1787 <code>publicId
</code>, and
<code>notationName
</code> will all be NULL.
1788 The value string is
<em>not
</em> NULL terminated; the length is
1789 provided in the
<code>value_length
</code> parameter. Do not use
1790 <code>value_length
</code> to test for internal entities, since it is
1791 legal to have zero-length values. Instead check for whether or not
1792 <code>value
</code> is NULL.
</p> <p>The
<code>notationName
</code>
1793 argument will have a non-NULL value only for unparsed entity
1797 <div class=
"handler">
1798 <pre class=
"setter" id=
"XML_SetUnparsedEntityDeclHandler">
1800 XML_SetUnparsedEntityDeclHandler(XML_Parser p,
1801 XML_UnparsedEntityDeclHandler h)
1803 <pre class=
"signature">
1805 (XMLCALL *XML_UnparsedEntityDeclHandler)(void *userData,
1806 const XML_Char *entityName,
1807 const XML_Char *base,
1808 const XML_Char *systemId,
1809 const XML_Char *publicId,
1810 const XML_Char *notationName);
1812 <p>Set a handler that receives declarations of unparsed entities. These
1813 are entity declarations that have a notation (NDATA) field:
</p>
1816 <!ENTITY logo SYSTEM
"images/logo.gif" NDATA gif
>
1818 <p>This handler is obsolete and is provided for backwards
1819 compatibility. Use instead
<a href=
"#XML_SetEntityDeclHandler"
1820 >XML_SetEntityDeclHandler
</a>.
</p>
1823 <div class=
"handler">
1824 <pre class=
"setter" id=
"XML_SetNotationDeclHandler">
1826 XML_SetNotationDeclHandler(XML_Parser p,
1827 XML_NotationDeclHandler h)
1829 <pre class=
"signature">
1831 (XMLCALL *XML_NotationDeclHandler)(void *userData,
1832 const XML_Char *notationName,
1833 const XML_Char *base,
1834 const XML_Char *systemId,
1835 const XML_Char *publicId);
1837 <p>Set a handler that receives notation declarations.
</p>
1840 <div class=
"handler">
1841 <pre class=
"setter" id=
"XML_SetNotStandaloneHandler">
1843 XML_SetNotStandaloneHandler(XML_Parser p,
1844 XML_NotStandaloneHandler h)
1846 <pre class=
"signature">
1848 (XMLCALL *XML_NotStandaloneHandler)(void *userData);
1850 <p>Set a handler that is called if the document is not
"standalone".
1851 This happens when there is an external subset or a reference to a
1852 parameter entity, but does not have standalone set to
"yes" in an XML
1853 declaration. If this handler returns
<code>XML_STATUS_ERROR
</code>,
1854 then the parser will throw an
<code>XML_ERROR_NOT_STANDALONE
</code>
1858 <h3><a name=
"position">Parse position and error reporting functions
</a></h3>
1860 <p>These are the functions you'll want to call when the parse
1861 functions return
<code>XML_STATUS_ERROR
</code> (a parse error has
1862 occurred), although the position reporting functions are useful outside
1863 of errors. The position reported is the byte position (in the original
1864 document or entity encoding) of the first of the sequence of
1865 characters that generated the current event (or the error that caused
1866 the parse functions to return
<code>XML_STATUS_ERROR
</code>.) The
1867 exceptions are callbacks trigged by declarations in the document
1868 prologue, in which case they exact position reported is somewhere in the
1869 relevant markup, but not necessarily as meaningful as for other
1872 <p>The position reporting functions are accurate only outside of the
1873 DTD. In other words, they usually return bogus information when
1874 called from within a DTD declaration handler.
</p>
1876 <pre class=
"fcndec" id=
"XML_GetErrorCode">
1877 enum XML_Error XMLCALL
1878 XML_GetErrorCode(XML_Parser p);
1880 <div class=
"fcndef">
1881 Return what type of error has occurred.
1884 <pre class=
"fcndec" id=
"XML_ErrorString">
1885 const XML_LChar * XMLCALL
1886 XML_ErrorString(enum XML_Error code);
1888 <div class=
"fcndef">
1889 Return a string describing the error corresponding to code.
1890 The code should be one of the enums that can be returned from
1891 <code><a href=
"#XML_GetErrorCode" >XML_GetErrorCode
</a></code>.
1894 <pre class=
"fcndec" id=
"XML_GetCurrentByteIndex">
1896 XML_GetCurrentByteIndex(XML_Parser p);
1898 <div class=
"fcndef">
1899 Return the byte offset of the position. This always corresponds to
1900 the values returned by
<code><a href=
"#XML_GetCurrentLineNumber"
1901 >XML_GetCurrentLineNumber
</a></code> and
<code><a href=
1902 "#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber
</a></code>.
1905 <pre class=
"fcndec" id=
"XML_GetCurrentLineNumber">
1907 XML_GetCurrentLineNumber(XML_Parser p);
1909 <div class=
"fcndef">
1910 Return the line number of the position. The first line is reported as
1914 <pre class=
"fcndec" id=
"XML_GetCurrentColumnNumber">
1916 XML_GetCurrentColumnNumber(XML_Parser p);
1918 <div class=
"fcndef">
1919 Return the offset, from the beginning of the current line, of
1923 <pre class=
"fcndec" id=
"XML_GetCurrentByteCount">
1925 XML_GetCurrentByteCount(XML_Parser p);
1927 <div class=
"fcndef">
1928 Return the number of bytes in the current event. Returns
1929 <code>0</code> if the event is inside a reference to an internal
1930 entity and for the end-tag event for empty element tags (the later can
1931 be used to distinguish empty-element tags from empty elements using
1932 separate start and end tags).
1935 <pre class=
"fcndec" id=
"XML_GetInputContext">
1936 const char * XMLCALL
1937 XML_GetInputContext(XML_Parser p,
1941 <div class=
"fcndef">
1943 <p>Returns the parser's input buffer, sets the integer pointed at by
1944 <code>offset
</code> to the offset within this buffer of the current
1945 parse position, and set the integer pointed at by
<code>size
</code> to
1946 the size of the returned buffer.
</p>
1948 <p>This should only be called from within a handler during an active
1949 parse and the returned buffer should only be referred to from within
1950 the handler that made the call. This input buffer contains the
1951 untranslated bytes of the input.
</p>
1953 <p>Only a limited amount of context is kept, so if the event
1954 triggering a call spans over a very large amount of input, the actual
1955 parse position may be before the beginning of the buffer.
</p>
1957 <p>If
<code>XML_CONTEXT_BYTES
</code> is not defined, this will always
1961 <h3><a name=
"miscellaneous">Miscellaneous functions
</a></h3>
1963 <p>The functions in this section either obtain state information from
1964 the parser or can be used to dynamicly set parser options.
</p>
1966 <pre class=
"fcndec" id=
"XML_SetUserData">
1968 XML_SetUserData(XML_Parser p,
1971 <div class=
"fcndef">
1972 This sets the user data pointer that gets passed to handlers. It
1973 overwrites any previous value for this pointer. Note that the
1974 application is responsible for freeing the memory associated with
1975 <code>userData
</code> when it is finished with the parser. So if you
1976 call this when there's already a pointer there, and you haven't freed
1977 the memory associated with it, then you've probably just leaked
1981 <pre class=
"fcndec" id=
"XML_GetUserData">
1983 XML_GetUserData(XML_Parser p);
1985 <div class=
"fcndef">
1986 This returns the user data pointer that gets passed to handlers.
1987 It is actually implemented as a macro.
1990 <pre class=
"fcndec" id=
"XML_UseParserAsHandlerArg">
1992 XML_UseParserAsHandlerArg(XML_Parser p);
1994 <div class=
"fcndef">
1995 After this is called, handlers receive the parser in their
1996 <code>userData
</code> arguments. The user data can still be obtained
1997 using the
<code><a href=
"#XML_GetUserData"
1998 >XML_GetUserData
</a></code> function.
2001 <pre class=
"fcndec" id=
"XML_SetBase">
2002 enum XML_Status XMLCALL
2003 XML_SetBase(XML_Parser p,
2004 const XML_Char *base);
2006 <div class=
"fcndef">
2007 Set the base to be used for resolving relative URIs in system
2008 identifiers. The return value is
<code>XML_STATUS_ERROR
</code> if
2009 there's no memory to store base, otherwise it's
2010 <code>XML_STATUS_OK
</code>.
2013 <pre class=
"fcndec" id=
"XML_GetBase">
2014 const XML_Char * XMLCALL
2015 XML_GetBase(XML_Parser p);
2017 <div class=
"fcndef">
2018 Return the base for resolving relative URIs.
2021 <pre class=
"fcndec" id=
"XML_GetSpecifiedAttributeCount">
2023 XML_GetSpecifiedAttributeCount(XML_Parser p);
2025 <div class=
"fcndef">
2026 When attributes are reported to the start handler in the atts vector,
2027 attributes that were explicitly set in the element occur before any
2028 attributes that receive their value from default information in an
2029 ATTLIST declaration. This function returns the number of attributes
2030 that were explicitly set times two, thus giving the offset in the
2031 <code>atts
</code> array passed to the start tag handler of the first
2032 attribute set due to defaults. It supplies information for the last
2033 call to a start handler. If called inside a start handler, then that
2034 means the current call.
2037 <pre class=
"fcndec" id=
"XML_GetIdAttributeIndex">
2039 XML_GetIdAttributeIndex(XML_Parser p);
2041 <div class=
"fcndef">
2042 Returns the index of the ID attribute passed in the atts array in the
2043 last call to
<code><a href=
"#XML_StartElementHandler"
2044 >XML_StartElementHandler
</a></code>, or -
1 if there is no ID
2045 attribute. If called inside a start handler, then that means the
2049 <pre class=
"fcndec" id=
"XML_SetEncoding">
2050 enum XML_Status XMLCALL
2051 XML_SetEncoding(XML_Parser p,
2052 const XML_Char *encoding);
2054 <div class=
"fcndef">
2055 Set the encoding to be used by the parser. It is equivalent to
2056 passing a non-null encoding argument to the parser creation functions.
2057 It must not be called after
<code><a href=
"#XML_Parse"
2058 >XML_Parse
</a></code> or
<code><a href=
"#XML_ParseBuffer"
2059 >XML_ParseBuffer
</a></code> have been called on the given parser.
2060 Returns
<code>XML_STATUS_OK
</code> on success or
2061 <code>XML_STATUS_ERROR
</code> on error.
2064 <pre class=
"fcndec" id=
"XML_SetParamEntityParsing">
2066 XML_SetParamEntityParsing(XML_Parser p,
2067 enum XML_ParamEntityParsing code);
2069 <div class=
"fcndef">
2070 This enables parsing of parameter entities, including the external
2071 parameter entity that is the external DTD subset, according to
2073 The choices for
<code>code
</code> are:
2075 <li><code>XML_PARAM_ENTITY_PARSING_NEVER
</code></li>
2076 <li><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
</code></li>
2077 <li><code>XML_PARAM_ENTITY_PARSING_ALWAYS
</code></li>
2081 <pre class=
"fcndec" id=
"XML_UseForeignDTD">
2082 enum XML_Error XMLCALL
2083 XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD);
2085 <div class=
"fcndef">
2086 <p>This function allows an application to provide an external subset
2087 for the document type declaration for documents which do not specify
2088 an external subset of their own. For documents which specify an
2089 external subset in their DOCTYPE declaration, the application-provided
2090 subset will be ignored. If the document does not contain a DOCTYPE
2091 declaration at all and
<code>useDTD
</code> is true, the
2092 application-provided subset will be parsed, but the
2093 <code>startDoctypeDeclHandler
</code> and
2094 <code>endDoctypeDeclHandler
</code> functions, if set, will not be
2095 called. The setting of parameter entity parsing, controlled using
2096 <code><a href=
"#XML_SetParamEntityParsing"
2097 >XML_SetParamEntityParsing
</a></code>, will be honored.
</p>
2099 <p>The application-provided external subset is read by calling the
2100 external entity reference handler set via
<code><a href=
2101 "#XML_SetExternalEntityRefHandler"
2102 >XML_SetExternalEntityRefHandler
</a></code> with both
2103 <code>publicId
</code> and
<code>systemId
</code> set to NULL.
</p>
2105 <p>If this function is called after parsing has begun, it returns
2106 <code>XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING
</code> and ignores
2107 <code>useDTD
</code>. If called when Expat has been compiled without
2108 DTD support, it returns
2109 <code>XML_ERROR_FEATURE_REQUIRES_XML_DTD
</code>. Otherwise, it
2110 returns
<code>XML_ERROR_NONE
</code>.
</p>
2112 <p><b>Note:
</b> For the purpose of checking WFC: Entity Declared, passing
2113 <code>useDTD == XML_TRUE
</code> will make the parser behave as if
2114 the document had a DTD with an external subset. This holds true even if
2115 the external entity reference handler returns without action.
</p>
2118 <pre class=
"fcndec" id=
"XML_SetReturnNSTriplet">
2120 XML_SetReturnNSTriplet(XML_Parser parser,
2123 <div class=
"fcndef">
2125 This function only has an effect when using a parser created with
2126 <code><a href=
"#XML_ParserCreateNS" >XML_ParserCreateNS
</a></code>,
2127 i.e. when namespace processing is in effect. The
<code>do_nst
</code>
2128 sets whether or not prefixes are returned with names qualified with a
2129 namespace prefix. If this function is called with
<code>do_nst
</code>
2130 non-zero, then afterwards namespace qualified names (that is qualified
2131 with a prefix as opposed to belonging to a default namespace) are
2132 returned as a triplet with the three parts separated by the namespace
2133 separator specified when the parser was created. The order of
2134 returned parts is URI, local name, and prefix.
</p> <p>If
2135 <code>do_nst
</code> is zero, then namespaces are reported in the
2136 default manner, URI then local_name separated by the namespace
2140 <pre class=
"fcndec" id=
"XML_DefaultCurrent">
2142 XML_DefaultCurrent(XML_Parser parser);
2144 <div class=
"fcndef">
2145 This can be called within a handler for a start element, end element,
2146 processing instruction or character data. It causes the corresponding
2147 markup to be passed to the default handler set by
<code><a
2148 href=
"#XML_SetDefaultHandler" >XML_SetDefaultHandler
</a></code> or
2149 <code><a href=
"#XML_SetDefaultHandlerExpand"
2150 >XML_SetDefaultHandlerExpand
</a></code>. It does nothing if there is
2151 not a default handler.
2154 <pre class=
"fcndec" id=
"XML_ExpatVersion">
2158 <div class=
"fcndef">
2159 Return the library version as a string (e.g.
<code>"expat_1.95.1"</code>).
2162 <pre class=
"fcndec" id=
"XML_ExpatVersionInfo">
2163 struct XML_Expat_Version XMLCALL
2164 XML_ExpatVersionInfo();
2166 <pre class=
"signature">
2171 } XML_Expat_Version;
2173 <div class=
"fcndef">
2174 Return the library version information as a structure.
2175 Some macros are also defined that support compile-time tests of the
2178 <li><code>XML_MAJOR_VERSION
</code></li>
2179 <li><code>XML_MINOR_VERSION
</code></li>
2180 <li><code>XML_MICRO_VERSION
</code></li>
2182 Testing these constants is currently the best way to determine if
2183 particular parts of the Expat API are available.
2186 <pre class=
"fcndec" id=
"XML_GetFeatureList">
2187 const XML_Feature * XMLCALL
2188 XML_GetFeatureList();
2190 <pre class=
"signature">
2191 enum XML_FeatureEnum {
2192 XML_FEATURE_END =
0,
2193 XML_FEATURE_UNICODE,
2194 XML_FEATURE_UNICODE_WCHAR_T,
2196 XML_FEATURE_CONTEXT_BYTES,
2197 XML_FEATURE_MIN_SIZE,
2198 XML_FEATURE_SIZEOF_XML_CHAR,
2199 XML_FEATURE_SIZEOF_XML_LCHAR
2203 enum XML_FeatureEnum feature;
2208 <div class=
"fcndef">
2209 <p>Returns a list of
"feature" records, providing details on how
2210 Expat was configured at compile time. Most applications should not
2211 need to worry about this, but this information is otherwise not
2212 available from Expat. This function allows code that does need to
2213 check these features to do so at runtime.
</p>
2215 <p>The return value is an array of
<code>XML_Feature
</code>,
2216 terminated by a record with a
<code>feature
</code> of
2217 <code>XML_FEATURE_END
</code> and
<code>name
</code> of NULL,
2218 identifying the feature-test macros Expat was compiled with. Since an
2219 application that requires this kind of information needs to determine
2220 the type of character the
<code>name
</code> points to, records for the
2221 <code>XML_FEATURE_SIZEOF_XML_CHAR
</code> and
2222 <code>XML_FEATURE_SIZEOF_XML_LCHAR
</code> will be located at the
2223 beginning of the list, followed by
<code>XML_FEATURE_UNICODE
</code>
2224 and
<code>XML_FEATURE_UNICODE_WCHAR_T
</code>, if they are present at
2227 <p>Some features have an associated value. If there isn't an
2228 associated value, the
<code>value
</code> field is set to
0. At this
2229 time, the following features have been defined to have values:
</p>
2232 <dt><code>XML_FEATURE_SIZEOF_XML_CHAR
</code></dt>
2233 <dd>The number of bytes occupied by one
<code>XML_Char
</code>
2235 <dt><code>XML_FEATURE_SIZEOF_XML_LCHAR
</code></dt>
2236 <dd>The number of bytes occupied by one
<code>XML_LChar
</code>
2238 <dt><code>XML_FEATURE_CONTEXT_BYTES
</code></dt>
2239 <dd>The maximum number of characters of context which can be
2240 reported by
<code><a href=
"#XML_GetInputContext"
2241 >XML_GetInputContext
</a></code>.
</dd>
2245 <pre class=
"fcndec" id=
"XML_FreeContentModel">
2247 XML_FreeContentModel(XML_Parser parser, XML_Content *model);
2249 <div class=
"fcndef">
2250 Function to deallocate the
<code>model
</code> argument passed to the
2251 <code>XML_ElementDeclHandler
</code> callback set using
<code><a
2252 href=
"#XML_SetElementDeclHandler" >XML_ElementDeclHandler
</a></code>.
2253 This function should not be used for any other purpose.
2256 <p>The following functions allow external code to share the memory
2257 allocator an
<code>XML_Parser
</code> has been configured to use. This
2258 is especially useful for third-party libraries that interact with a
2259 parser object created by application code, or heavily layered
2260 applications. This can be essential when using dynamically loaded
2261 libraries which use different C standard libraries (this can happen on
2262 Windows, at least).
</p>
2264 <pre class=
"fcndec" id=
"XML_MemMalloc">
2266 XML_MemMalloc(XML_Parser parser, size_t size);
2268 <div class=
"fcndef">
2269 Allocate
<code>size
</code> bytes of memory using the allocator the
2270 <code>parser
</code> object has been configured to use. Returns a
2271 pointer to the memory or NULL on failure. Memory allocated in this
2272 way must be freed using
<code><a href=
"#XML_MemFree"
2273 >XML_MemFree
</a></code>.
2276 <pre class=
"fcndec" id=
"XML_MemRealloc">
2278 XML_MemRealloc(XML_Parser parser, void *ptr, size_t size);
2280 <div class=
"fcndef">
2281 Allocate
<code>size
</code> bytes of memory using the allocator the
2282 <code>parser
</code> object has been configured to use.
2283 <code>ptr
</code> must point to a block of memory allocated by
<code><a
2284 href=
"#XML_MemMalloc" >XML_MemMalloc
</a></code> or
2285 <code>XML_MemRealloc
</code>, or be NULL. This function tries to
2286 expand the block pointed to by
<code>ptr
</code> if possible. Returns
2287 a pointer to the memory or NULL on failure. On success, the original
2288 block has either been expanded or freed. On failure, the original
2289 block has not been freed; the caller is responsible for freeing the
2290 original block. Memory allocated in this way must be freed using
2291 <code><a href=
"#XML_MemFree"
2292 >XML_MemFree
</a></code>.
2295 <pre class=
"fcndec" id=
"XML_MemFree">
2297 XML_MemFree(XML_Parser parser, void *ptr);
2299 <div class=
"fcndef">
2300 Free a block of memory pointed to by
<code>ptr
</code>. The block must
2301 have been allocated by
<code><a href=
"#XML_MemMalloc"
2302 >XML_MemMalloc
</a></code> or
<code>XML_MemRealloc
</code>, or be NULL.
2306 <p><a href=
"http://validator.w3.org/check/referer"><img
2307 src=
"valid-xhtml10.png" alt=
"Valid XHTML 1.0!"
2308 height=
"31" width=
"88" class=
"noborder" /></a></p>