reference.html revision 1.1.1.6 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">
4 <html>
5 <head>
6 <!--
7 __ __ _
8 ___\ \/ /_ __ __ _| |_
9 / _ \\ /| '_ \ / _` | __|
10 | __// \| |_) | (_| | |_
11 \___/_/\_\ .__/ \__,_|\__|
12 |_| XML parser
13
14 Copyright (c) 2000 Clark Cooper <coopercc (a] users.sourceforge.net>
15 Copyright (c) 2000-2004 Fred L. Drake, Jr. <fdrake (a] users.sourceforge.net>
16 Copyright (c) 2002-2012 Karl Waclawek <karl (a] waclawek.net>
17 Copyright (c) 2017-2022 Sebastian Pipping <sebastian (a] pipping.org>
18 Copyright (c) 2017 Jakub Wilk <jwilk (a] jwilk.net>
19 Copyright (c) 2021 Tomas Korbar <tkorbar (a] redhat.com>
20 Copyright (c) 2021 Nicolas Cavallari <nicolas.cavallari (a] green-communications.fr>
21 Licensed under the MIT license:
22
23 Permission is hereby granted, free of charge, to any person obtaining
24 a copy of this software and associated documentation files (the
25 "Software"), to deal in the Software without restriction, including
26 without limitation the rights to use, copy, modify, merge, publish,
27 distribute, sublicense, and/or sell copies of the Software, and to permit
28 persons to whom the Software is furnished to do so, subject to the
29 following conditions:
30
31 The above copyright notice and this permission notice shall be included
32 in all copies or substantial portions of the Software.
33
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
37 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
38 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
39 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
40 USE OR OTHER DEALINGS IN THE SOFTWARE.
41 -->
42 <title>Expat XML Parser</title>
43 <meta name="author" content="Clark Cooper, coopercc (a] netheaven.com" />
44 <meta http-equiv="Content-Style-Type" content="text/css" />
45 <link href="ok.min.css" rel="stylesheet" type="text/css" />
46 <link href="style.css" rel="stylesheet" type="text/css" />
47 </head>
48 <body>
49 <div>
50 <h1>
51 The Expat XML Parser
52 <small>Release 2.4.6</small>
53 </h1>
54 </div>
55 <div class="content">
56
57 <p>Expat is a library, written in C, for parsing XML documents. It's
58 the underlying XML parser for the open source Mozilla project, Perl's
59 <code>XML::Parser</code>, Python's <code>xml.parsers.expat</code>, and
60 other open-source XML parsers.</p>
61
62 <p>This library is the creation of James Clark, who's also given us
63 groff (an nroff look-alike), Jade (an implementation of ISO's DSSSL
64 stylesheet language for SGML), XP (a Java XML parser package), XT (a
65 Java XSL engine). James was also the technical lead on the XML
66 Working Group at W3C that produced the XML specification.</p>
67
68 <p>This is free software, licensed under the <a
69 href="../COPYING">MIT/X Consortium license</a>. You may download it
70 from <a href="http://www.libexpat.org/">the Expat home page</a>.
71 </p>
72
73 <p>The bulk of this document was originally commissioned as an article
74 by <a href="http://www.xml.com/">XML.com</a>. They graciously allowed
75 Clark Cooper to retain copyright and to distribute it with Expat.
76 This version has been substantially extended to include documentation
77 on features which have been added since the original article was
78 published, and additional information on using the original
79 interface.</p>
80
81 <hr />
82 <h2>Table of Contents</h2>
83 <ul>
84 <li><a href="#overview">Overview</a></li>
85 <li><a href="#building">Building and Installing</a></li>
86 <li><a href="#using">Using Expat</a></li>
87 <li><a href="#reference">Reference</a>
88 <ul>
89 <li><a href="#creation">Parser Creation Functions</a>
90 <ul>
91 <li><a href="#XML_ParserCreate">XML_ParserCreate</a></li>
92 <li><a href="#XML_ParserCreateNS">XML_ParserCreateNS</a></li>
93 <li><a href="#XML_ParserCreate_MM">XML_ParserCreate_MM</a></li>
94 <li><a href="#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></li>
95 <li><a href="#XML_ParserFree">XML_ParserFree</a></li>
96 <li><a href="#XML_ParserReset">XML_ParserReset</a></li>
97 </ul>
98 </li>
99 <li><a href="#parsing">Parsing Functions</a>
100 <ul>
101 <li><a href="#XML_Parse">XML_Parse</a></li>
102 <li><a href="#XML_ParseBuffer">XML_ParseBuffer</a></li>
103 <li><a href="#XML_GetBuffer">XML_GetBuffer</a></li>
104 <li><a href="#XML_StopParser">XML_StopParser</a></li>
105 <li><a href="#XML_ResumeParser">XML_ResumeParser</a></li>
106 <li><a href="#XML_GetParsingStatus">XML_GetParsingStatus</a></li>
107 </ul>
108 </li>
109 <li><a href="#setting">Handler Setting Functions</a>
110 <ul>
111 <li><a href="#XML_SetStartElementHandler">XML_SetStartElementHandler</a></li>
112 <li><a href="#XML_SetEndElementHandler">XML_SetEndElementHandler</a></li>
113 <li><a href="#XML_SetElementHandler">XML_SetElementHandler</a></li>
114 <li><a href="#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a></li>
115 <li><a href="#XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</a></li>
116 <li><a href="#XML_SetCommentHandler">XML_SetCommentHandler</a></li>
117 <li><a href="#XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</a></li>
118 <li><a href="#XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</a></li>
119 <li><a href="#XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</a></li>
120 <li><a href="#XML_SetDefaultHandler">XML_SetDefaultHandler</a></li>
121 <li><a href="#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</a></li>
122 <li><a href="#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></li>
123 <li><a href="#XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</a></li>
124 <li><a href="#XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</a></li>
125 <li><a href="#XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</a></li>
126 <li><a href="#XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</a></li>
127 <li><a href="#XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</a></li>
128 <li><a href="#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</a></li>
129 <li><a href="#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</a></li>
130 <li><a href="#XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</a></li>
131 <li><a href="#XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</a></li>
132 <li><a href="#XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</a></li>
133 <li><a href="#XML_SetElementDeclHandler">XML_SetElementDeclHandler</a></li>
134 <li><a href="#XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</a></li>
135 <li><a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a></li>
136 <li><a href="#XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</a></li>
137 <li><a href="#XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</a></li>
138 <li><a href="#XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</a></li>
139 </ul>
140 </li>
141 <li><a href="#position">Parse Position and Error Reporting Functions</a>
142 <ul>
143 <li><a href="#XML_GetErrorCode">XML_GetErrorCode</a></li>
144 <li><a href="#XML_ErrorString">XML_ErrorString</a></li>
145 <li><a href="#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</a></li>
146 <li><a href="#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a></li>
147 <li><a href="#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a></li>
148 <li><a href="#XML_GetCurrentByteCount">XML_GetCurrentByteCount</a></li>
149 <li><a href="#XML_GetInputContext">XML_GetInputContext</a></li>
150 </ul>
151 </li>
152 <li>
153 <a href="#billion-laughs">Billion Laughs Attack Protection</a>
154 <ul>
155 <li><a href="#XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></li>
156 <li><a href="#XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</a></li>
157 </ul>
158 </li>
159 <li><a href="#miscellaneous">Miscellaneous Functions</a>
160 <ul>
161 <li><a href="#XML_SetUserData">XML_SetUserData</a></li>
162 <li><a href="#XML_GetUserData">XML_GetUserData</a></li>
163 <li><a href="#XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</a></li>
164 <li><a href="#XML_SetBase">XML_SetBase</a></li>
165 <li><a href="#XML_GetBase">XML_GetBase</a></li>
166 <li><a href="#XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</a></li>
167 <li><a href="#XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</a></li>
168 <li><a href="#XML_GetAttributeInfo">XML_GetAttributeInfo</a></li>
169 <li><a href="#XML_SetEncoding">XML_SetEncoding</a></li>
170 <li><a href="#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a></li>
171 <li><a href="#XML_SetHashSalt">XML_SetHashSalt</a></li>
172 <li><a href="#XML_UseForeignDTD">XML_UseForeignDTD</a></li>
173 <li><a href="#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a></li>
174 <li><a href="#XML_DefaultCurrent">XML_DefaultCurrent</a></li>
175 <li><a href="#XML_ExpatVersion">XML_ExpatVersion</a></li>
176 <li><a href="#XML_ExpatVersionInfo">XML_ExpatVersionInfo</a></li>
177 <li><a href="#XML_GetFeatureList">XML_GetFeatureList</a></li>
178 <li><a href="#XML_FreeContentModel">XML_FreeContentModel</a></li>
179 <li><a href="#XML_MemMalloc">XML_MemMalloc</a></li>
180 <li><a href="#XML_MemRealloc">XML_MemRealloc</a></li>
181 <li><a href="#XML_MemFree">XML_MemFree</a></li>
182 </ul>
183 </li>
184 </ul>
185 </li>
186 </ul>
187
188 <hr />
189 <h2><a name="overview">Overview</a></h2>
190
191 <p>Expat is a stream-oriented parser. You register callback (or
192 handler) functions with the parser and then start feeding it the
193 document. As the parser recognizes parts of the document, it will
194 call the appropriate handler for that part (if you've registered one.)
195 The document is fed to the parser in pieces, so you can start parsing
196 before you have all the document. This also allows you to parse really
197 huge documents that won't fit into memory.</p>
198
199 <p>Expat can be intimidating due to the many kinds of handlers and
200 options you can set. But you only need to learn four functions in
201 order to do 90% of what you'll want to do with it:</p>
202
203 <dl>
204
205 <dt><code><a href= "#XML_ParserCreate"
206 >XML_ParserCreate</a></code></dt>
207 <dd>Create a new parser object.</dd>
208
209 <dt><code><a href= "#XML_SetElementHandler"
210 >XML_SetElementHandler</a></code></dt>
211 <dd>Set handlers for start and end tags.</dd>
212
213 <dt><code><a href= "#XML_SetCharacterDataHandler"
214 >XML_SetCharacterDataHandler</a></code></dt>
215 <dd>Set handler for text.</dd>
216
217 <dt><code><a href= "#XML_Parse"
218 >XML_Parse</a></code></dt>
219 <dd>Pass a buffer full of document to the parser</dd>
220 </dl>
221
222 <p>These functions and others are described in the <a
223 href="#reference">reference</a> part of this document. The reference
224 section also describes in detail the parameters passed to the
225 different types of handlers.</p>
226
227 <p>Let's look at a very simple example program that only uses 3 of the
228 above functions (it doesn't need to set a character handler.) The
229 program <a href="../examples/outline.c">outline.c</a> prints an
230 element outline, indenting child elements to distinguish them from the
231 parent element that contains them. The start handler does all the
232 work. It prints two indenting spaces for every level of ancestor
233 elements, then it prints the element and attribute
234 information. Finally it increments the global <code>Depth</code>
235 variable.</p>
236
237 <pre class="eg">
238 int Depth;
239
240 void XMLCALL
241 start(void *data, const char *el, const char **attr) {
242 int i;
243
244 for (i = 0; i < Depth; i++)
245 printf(" ");
246
247 printf("%s", el);
248
249 for (i = 0; attr[i]; i += 2) {
250 printf(" %s='%s'", attr[i], attr[i + 1]);
251 }
252
253 printf("\n");
254 Depth++;
255 } /* End of start handler */
256 </pre>
257
258 <p>The end tag simply does the bookkeeping work of decrementing
259 <code>Depth</code>.</p>
260 <pre class="eg">
261 void XMLCALL
262 end(void *data, const char *el) {
263 Depth--;
264 } /* End of end handler */
265 </pre>
266
267 <p>Note the <code>XMLCALL</code> annotation used for the callbacks.
268 This is used to ensure that the Expat and the callbacks are using the
269 same calling convention in case the compiler options used for Expat
270 itself and the client code are different. Expat tries not to care
271 what the default calling convention is, though it may require that it
272 be compiled with a default convention of "cdecl" on some platforms.
273 For code which uses Expat, however, the calling convention is
274 specified by the <code>XMLCALL</code> annotation on most platforms;
275 callbacks should be defined using this annotation.</p>
276
277 <p>The <code>XMLCALL</code> annotation was added in Expat 1.95.7, but
278 existing working Expat applications don't need to add it (since they
279 are already using the "cdecl" calling convention, or they wouldn't be
280 working). The annotation is only needed if the default calling
281 convention may be something other than "cdecl". To use the annotation
282 safely with older versions of Expat, you can conditionally define it
283 <em>after</em> including Expat's header file:</p>
284
285 <pre class="eg">
286 #include <expat.h>
287
288 #ifndef XMLCALL
289 #if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) && !defined(__CYGWIN__)
290 #define XMLCALL __cdecl
291 #elif defined(__GNUC__)
292 #define XMLCALL __attribute__((cdecl))
293 #else
294 #define XMLCALL
295 #endif
296 #endif
297 </pre>
298
299 <p>After creating the parser, the main program just has the job of
300 shoveling the document to the parser so that it can do its work.</p>
301
302 <hr />
303 <h2><a name="building">Building and Installing Expat</a></h2>
304
305 <p>The Expat distribution comes as a compressed (with GNU gzip) tar
306 file. You may download the latest version from <a href=
307 "http://sourceforge.net/projects/expat/" >Source Forge</a>. After
308 unpacking this, cd into the directory. Then follow either the Win32
309 directions or Unix directions below.</p>
310
311 <h3>Building under Win32</h3>
312
313 <p>If you're using the GNU compiler under cygwin, follow the Unix
314 directions in the next section. Otherwise if you have Microsoft's
315 Developer Studio installed,
316 you can use CMake to generate a <code>.sln</code> file, e.g.
317 <code>
318 cmake -G"Visual Studio 15 2017" -DCMAKE_BUILD_TYPE=RelWithDebInfo .
319 </code>, and build Expat using <code>msbuild /m expat.sln</code> after.</p>
320
321 <p>Alternatively, you may download the Win32 binary package that
322 contains the "expat.h" include file and a pre-built DLL.</p>
323
324 <h3>Building under Unix (or GNU)</h3>
325
326 <p>First you'll need to run the configure shell script in order to
327 configure the Makefiles and headers for your system.</p>
328
329 <p>If you're happy with all the defaults that configure picks for you,
330 and you have permission on your system to install into /usr/local, you
331 can install Expat with this sequence of commands:</p>
332
333 <pre class="eg">
334 ./configure
335 make
336 make install
337 </pre>
338
339 <p>There are some options that you can provide to this script, but the
340 only one we'll mention here is the <code>--prefix</code> option. You
341 can find out all the options available by running configure with just
342 the <code>--help</code> option.</p>
343
344 <p>By default, the configure script sets things up so that the library
345 gets installed in <code>/usr/local/lib</code> and the associated
346 header file in <code>/usr/local/include</code>. But if you were to
347 give the option, <code>--prefix=/home/me/mystuff</code>, then the
348 library and header would get installed in
349 <code>/home/me/mystuff/lib</code> and
350 <code>/home/me/mystuff/include</code> respectively.</p>
351
352 <h3>Configuring Expat Using the Pre-Processor</h3>
353
354 <p>Expat's feature set can be configured using a small number of
355 pre-processor definitions. The definition of this symbols does not
356 affect the set of entry points for Expat, only the behavior of the API
357 and the definition of character types in the case of
358 <code>XML_UNICODE_WCHAR_T</code>. The symbols are:</p>
359
360 <dl class="cpp-symbols">
361 <dt>XML_DTD</dt>
362 <dd>Include support for using and reporting DTD-based content. If
363 this is defined, default attribute values from an external DTD subset
364 are reported and attribute value normalization occurs based on the
365 type of attributes defined in the external subset. Without
366 this, Expat has a smaller memory footprint and can be faster, but will
367 not load external entities or process conditional sections. This does
368 not affect the set of functions available in the API.</dd>
369
370 <dt>XML_NS</dt>
371 <dd>When defined, support for the <cite><a href=
372 "http://www.w3.org/TR/REC-xml-names/" >Namespaces in XML</a></cite>
373 specification is included.</dd>
374
375 <dt>XML_UNICODE</dt>
376 <dd>When defined, character data reported to the application is
377 encoded in UTF-16 using wide characters of the type
378 <code>XML_Char</code>. This is implied if
379 <code>XML_UNICODE_WCHAR_T</code> is defined.</dd>
380
381 <dt>XML_UNICODE_WCHAR_T</dt>
382 <dd>If defined, causes the <code>XML_Char</code> character type to be
383 defined using the <code>wchar_t</code> type; otherwise, <code>unsigned
384 short</code> is used. Defining this implies
385 <code>XML_UNICODE</code>.</dd>
386
387 <dt>XML_LARGE_SIZE</dt>
388 <dd>If defined, causes the <code>XML_Size</code> and <code>XML_Index</code>
389 integer types to be at least 64 bits in size. This is intended to support
390 processing of very large input streams, where the return values of
391 <code><a href="#XML_GetCurrentByteIndex" >XML_GetCurrentByteIndex</a></code>,
392 <code><a href="#XML_GetCurrentLineNumber" >XML_GetCurrentLineNumber</a></code> and
393 <code><a href="#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber</a></code>
394 could overflow. It may not be supported by all compilers, and is turned
395 off by default.</dd>
396
397 <dt>XML_CONTEXT_BYTES</dt>
398 <dd>The number of input bytes of markup context which the parser will
399 ensure are available for reporting via <code><a href=
400 "#XML_GetInputContext" >XML_GetInputContext</a></code>. This is
401 normally set to 1024, and must be set to a positive integer. If this
402 is not defined, the input context will not be available and <code><a
403 href= "#XML_GetInputContext" >XML_GetInputContext</a></code> will
404 always report NULL. Without this, Expat has a smaller memory
405 footprint and can be faster.</dd>
406
407 <dt>XML_STATIC</dt>
408 <dd>On Windows, this should be set if Expat is going to be linked
409 statically with the code that calls it; this is required to get all
410 the right MSVC magic annotations correct. This is ignored on other
411 platforms.</dd>
412
413 <dt>XML_ATTR_INFO</dt>
414 <dd>If defined, makes the additional function <code><a href=
415 "#XML_GetAttributeInfo" >XML_GetAttributeInfo</a></code> available
416 for reporting attribute byte offsets.</dd>
417 </dl>
418
419 <hr />
420 <h2><a name="using">Using Expat</a></h2>
421
422 <h3>Compiling and Linking Against Expat</h3>
423
424 <p>Unless you installed Expat in a location not expected by your
425 compiler and linker, all you have to do to use Expat in your programs
426 is to include the Expat header (<code>#include <expat.h></code>)
427 in your files that make calls to it and to tell the linker that it
428 needs to link against the Expat library. On Unix systems, this would
429 usually be done with the <code>-lexpat</code> argument. Otherwise,
430 you'll need to tell the compiler where to look for the Expat header
431 and the linker where to find the Expat library. You may also need to
432 take steps to tell the operating system where to find this library at
433 run time.</p>
434
435 <p>On a Unix-based system, here's what a Makefile might look like when
436 Expat is installed in a standard location:</p>
437
438 <pre class="eg">
439 CC=cc
440 LDFLAGS=
441 LIBS= -lexpat
442 xmlapp: xmlapp.o
443 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)
444 </pre>
445
446 <p>If you installed Expat in, say, <code>/home/me/mystuff</code>, then
447 the Makefile would look like this:</p>
448
449 <pre class="eg">
450 CC=cc
451 CFLAGS= -I/home/me/mystuff/include
452 LDFLAGS=
453 LIBS= -L/home/me/mystuff/lib -lexpat
454 xmlapp: xmlapp.o
455 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)
456 </pre>
457
458 <p>You'd also have to set the environment variable
459 <code>LD_LIBRARY_PATH</code> to <code>/home/me/mystuff/lib</code> (or
460 to <code>${LD_LIBRARY_PATH}:/home/me/mystuff/lib</code> if
461 LD_LIBRARY_PATH already has some directories in it) in order to run
462 your application.</p>
463
464 <h3>Expat Basics</h3>
465
466 <p>As we saw in the example in the overview, the first step in parsing
467 an XML document with Expat is to create a parser object. There are <a
468 href="#creation">three functions</a> in the Expat API for creating a
469 parser object. However, only two of these (<code><a href=
470 "#XML_ParserCreate" >XML_ParserCreate</a></code> and <code><a href=
471 "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>) can be used for
472 constructing a parser for a top-level document. The object returned
473 by these functions is an opaque pointer (i.e. "expat.h" declares it as
474 void *) to data with further internal structure. In order to free the
475 memory associated with this object you must call <code><a href=
476 "#XML_ParserFree" >XML_ParserFree</a></code>. Note that if you have
477 provided any <a href="#userdata">user data</a> that gets stored in the
478 parser, then your application is responsible for freeing it prior to
479 calling <code>XML_ParserFree</code>.</p>
480
481 <p>The objects returned by the parser creation functions are good for
482 parsing only one XML document or external parsed entity. If your
483 application needs to parse many XML documents, then it needs to create
484 a parser object for each one. The best way to deal with this is to
485 create a higher level object that contains all the default
486 initialization you want for your parser objects.</p>
487
488 <p>Walking through a document hierarchy with a stream oriented parser
489 will require a good stack mechanism in order to keep track of current
490 context. For instance, to answer the simple question, "What element
491 does this text belong to?" requires a stack, since the parser may have
492 descended into other elements that are children of the current one and
493 has encountered this text on the way out.</p>
494
495 <p>The things you're likely to want to keep on a stack are the
496 currently opened element and it's attributes. You push this
497 information onto the stack in the start handler and you pop it off in
498 the end handler.</p>
499
500 <p>For some tasks, it is sufficient to just keep information on what
501 the depth of the stack is (or would be if you had one.) The outline
502 program shown above presents one example. Another such task would be
503 skipping over a complete element. When you see the start tag for the
504 element you want to skip, you set a skip flag and record the depth at
505 which the element started. When the end tag handler encounters the
506 same depth, the skipped element has ended and the flag may be
507 cleared. If you follow the convention that the root element starts at
508 1, then you can use the same variable for skip flag and skip
509 depth.</p>
510
511 <pre class="eg">
512 void
513 init_info(Parseinfo *info) {
514 info->skip = 0;
515 info->depth = 1;
516 /* Other initializations here */
517 } /* End of init_info */
518
519 void XMLCALL
520 rawstart(void *data, const char *el, const char **attr) {
521 Parseinfo *inf = (Parseinfo *) data;
522
523 if (! inf->skip) {
524 if (should_skip(inf, el, attr)) {
525 inf->skip = inf->depth;
526 }
527 else
528 start(inf, el, attr); /* This does rest of start handling */
529 }
530
531 inf->depth++;
532 } /* End of rawstart */
533
534 void XMLCALL
535 rawend(void *data, const char *el) {
536 Parseinfo *inf = (Parseinfo *) data;
537
538 inf->depth--;
539
540 if (! inf->skip)
541 end(inf, el); /* This does rest of end handling */
542
543 if (inf->skip == inf->depth)
544 inf->skip = 0;
545 } /* End rawend */
546 </pre>
547
548 <p>Notice in the above example the difference in how depth is
549 manipulated in the start and end handlers. The end tag handler should
550 be the mirror image of the start tag handler. This is necessary to
551 properly model containment. Since, in the start tag handler, we
552 incremented depth <em>after</em> the main body of start tag code, then
553 in the end handler, we need to manipulate it <em>before</em> the main
554 body. If we'd decided to increment it first thing in the start
555 handler, then we'd have had to decrement it last thing in the end
556 handler.</p>
557
558 <h3 id="userdata">Communicating between handlers</h3>
559
560 <p>In order to be able to pass information between different handlers
561 without using globals, you'll need to define a data structure to hold
562 the shared variables. You can then tell Expat (with the <code><a href=
563 "#XML_SetUserData" >XML_SetUserData</a></code> function) to pass a
564 pointer to this structure to the handlers. This is the first
565 argument received by most handlers. In the <a href="#reference"
566 >reference section</a>, an argument to a callback function is named
567 <code>userData</code> and have type <code>void *</code> if the user
568 data is passed; it will have the type <code>XML_Parser</code> if the
569 parser itself is passed. When the parser is passed, the user data may
570 be retrieved using <code><a href="#XML_GetUserData"
571 >XML_GetUserData</a></code>.</p>
572
573 <p>One common case where multiple calls to a single handler may need
574 to communicate using an application data structure is the case when
575 content passed to the character data handler (set by <code><a href=
576 "#XML_SetCharacterDataHandler"
577 >XML_SetCharacterDataHandler</a></code>) needs to be accumulated. A
578 common first-time mistake with any of the event-oriented interfaces to
579 an XML parser is to expect all the text contained in an element to be
580 reported by a single call to the character data handler. Expat, like
581 many other XML parsers, reports such data as a sequence of calls;
582 there's no way to know when the end of the sequence is reached until a
583 different callback is made. A buffer referenced by the user data
584 structure proves both an effective and convenient place to accumulate
585 character data.</p>
586
587 <!-- XXX example needed here -->
588
589
590 <h3>XML Version</h3>
591
592 <p>Expat is an XML 1.0 parser, and as such never complains based on
593 the value of the <code>version</code> pseudo-attribute in the XML
594 declaration, if present.</p>
595
596 <p>If an application needs to check the version number (to support
597 alternate processing), it should use the <code><a href=
598 "#XML_SetXmlDeclHandler" >XML_SetXmlDeclHandler</a></code> function to
599 set a handler that uses the information in the XML declaration to
600 determine what to do. This example shows how to check that only a
601 version number of <code>"1.0"</code> is accepted:</p>
602
603 <pre class="eg">
604 static int wrong_version;
605 static XML_Parser parser;
606
607 static void XMLCALL
608 xmldecl_handler(void *userData,
609 const XML_Char *version,
610 const XML_Char *encoding,
611 int standalone)
612 {
613 static const XML_Char Version_1_0[] = {'1', '.', '0', 0};
614
615 int i;
616
617 for (i = 0; i < (sizeof(Version_1_0) / sizeof(Version_1_0[0])); ++i) {
618 if (version[i] != Version_1_0[i]) {
619 wrong_version = 1;
620 /* also clear all other handlers: */
621 XML_SetCharacterDataHandler(parser, NULL);
622 ...
623 return;
624 }
625 }
626 ...
627 }
628 </pre>
629
630 <h3>Namespace Processing</h3>
631
632 <p>When the parser is created using the <code><a href=
633 "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>, function, Expat
634 performs namespace processing. Under namespace processing, Expat
635 consumes <code>xmlns</code> and <code>xmlns:...</code> attributes,
636 which declare namespaces for the scope of the element in which they
637 occur. This means that your start handler will not see these
638 attributes. Your application can still be informed of these
639 declarations by setting namespace declaration handlers with <a href=
640 "#XML_SetNamespaceDeclHandler"
641 ><code>XML_SetNamespaceDeclHandler</code></a>.</p>
642
643 <p>Element type and attribute names that belong to a given namespace
644 are passed to the appropriate handler in expanded form. By default
645 this expanded form is a concatenation of the namespace URI, the
646 separator character (which is the 2nd argument to <code><a href=
647 "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>), and the local
648 name (i.e. the part after the colon). Names with undeclared prefixes
649 are not well-formed when namespace processing is enabled, and will
650 trigger an error. Unprefixed attribute names are never expanded,
651 and unprefixed element names are only expanded when they are in the
652 scope of a default namespace.</p>
653
654 <p>However if <code><a href= "#XML_SetReturnNSTriplet"
655 >XML_SetReturnNSTriplet</a></code> has been called with a non-zero
656 <code>do_nst</code> parameter, then the expanded form for names with
657 an explicit prefix is a concatenation of: URI, separator, local name,
658 separator, prefix.</p>
659
660 <p>You can set handlers for the start of a namespace declaration and
661 for the end of a scope of a declaration with the <code><a href=
662 "#XML_SetNamespaceDeclHandler" >XML_SetNamespaceDeclHandler</a></code>
663 function. The StartNamespaceDeclHandler is called prior to the start
664 tag handler and the EndNamespaceDeclHandler is called after the
665 corresponding end tag that ends the namespace's scope. The namespace
666 start handler gets passed the prefix and URI for the namespace. For a
667 default namespace declaration (xmlns='...'), the prefix will be null.
668 The URI will be null for the case where the default namespace is being
669 unset. The namespace end handler just gets the prefix for the closing
670 scope.</p>
671
672 <p>These handlers are called for each declaration. So if, for
673 instance, a start tag had three namespace declarations, then the
674 StartNamespaceDeclHandler would be called three times before the start
675 tag handler is called, once for each declaration.</p>
676
677 <h3>Character Encodings</h3>
678
679 <p>While XML is based on Unicode, and every XML processor is required
680 to recognized UTF-8 and UTF-16 (1 and 2 byte encodings of Unicode),
681 other encodings may be declared in XML documents or entities. For the
682 main document, an XML declaration may contain an encoding
683 declaration:</p>
684 <pre>
685 <?xml version="1.0" encoding="ISO-8859-2"?>
686 </pre>
687
688 <p>External parsed entities may begin with a text declaration, which
689 looks like an XML declaration with just an encoding declaration:</p>
690 <pre>
691 <?xml encoding="Big5"?>
692 </pre>
693
694 <p>With Expat, you may also specify an encoding at the time of
695 creating a parser. This is useful when the encoding information may
696 come from a source outside the document itself (like a higher level
697 protocol.)</p>
698
699 <p><a name="builtin_encodings"></a>There are four built-in encodings
700 in Expat:</p>
701 <ul>
702 <li>UTF-8</li>
703 <li>UTF-16</li>
704 <li>ISO-8859-1</li>
705 <li>US-ASCII</li>
706 </ul>
707
708 <p>Anything else discovered in an encoding declaration or in the
709 protocol encoding specified in the parser constructor, triggers a call
710 to the <code>UnknownEncodingHandler</code>. This handler gets passed
711 the encoding name and a pointer to an <code>XML_Encoding</code> data
712 structure. Your handler must fill in this structure and return
713 <code>XML_STATUS_OK</code> if it knows how to deal with the
714 encoding. Otherwise the handler should return
715 <code>XML_STATUS_ERROR</code>. The handler also gets passed a pointer
716 to an optional application data structure that you may indicate when
717 you set the handler.</p>
718
719 <p>Expat places restrictions on character encodings that it can
720 support by filling in the <code>XML_Encoding</code> structure.
721 include file:</p>
722 <ol>
723 <li>Every ASCII character that can appear in a well-formed XML document
724 must be represented by a single byte, and that byte must correspond to
725 it's ASCII encoding (except for the characters $@\^'{}~)</li>
726 <li>Characters must be encoded in 4 bytes or less.</li>
727 <li>All characters encoded must have Unicode scalar values less than or
728 equal to 65535 (0xFFFF)<em>This does not apply to the built-in support
729 for UTF-16 and UTF-8</em></li>
730 <li>No character may be encoded by more that one distinct sequence of
731 bytes</li>
732 </ol>
733
734 <p><code>XML_Encoding</code> contains an array of integers that
735 correspond to the 1st byte of an encoding sequence. If the value in
736 the array for a byte is zero or positive, then the byte is a single
737 byte encoding that encodes the Unicode scalar value contained in the
738 array. A -1 in this array indicates a malformed byte. If the value is
739 -2, -3, or -4, then the byte is the beginning of a 2, 3, or 4 byte
740 sequence respectively. Multi-byte sequences are sent to the convert
741 function pointed at in the <code>XML_Encoding</code> structure. This
742 function should return the Unicode scalar value for the sequence or -1
743 if the sequence is malformed.</p>
744
745 <p>One pitfall that novice Expat users are likely to fall into is that
746 although Expat may accept input in various encodings, the strings that
747 it passes to the handlers are always encoded in UTF-8 or UTF-16
748 (depending on how Expat was compiled). Your application is responsible
749 for any translation of these strings into other encodings.</p>
750
751 <h3>Handling External Entity References</h3>
752
753 <p>Expat does not read or parse external entities directly. Note that
754 any external DTD is a special case of an external entity. If you've
755 set no <code>ExternalEntityRefHandler</code>, then external entity
756 references are silently ignored. Otherwise, it calls your handler with
757 the information needed to read and parse the external entity.</p>
758
759 <p>Your handler isn't actually responsible for parsing the entity, but
760 it is responsible for creating a subsidiary parser with <code><a href=
761 "#XML_ExternalEntityParserCreate"
762 >XML_ExternalEntityParserCreate</a></code> that will do the job. This
763 returns an instance of <code>XML_Parser</code> that has handlers and
764 other data structures initialized from the parent parser. You may then
765 use <code><a href= "#XML_Parse" >XML_Parse</a></code> or <code><a
766 href= "#XML_ParseBuffer">XML_ParseBuffer</a></code> calls against this
767 parser. Since external entities my refer to other external entities,
768 your handler should be prepared to be called recursively.</p>
769
770 <h3>Parsing DTDs</h3>
771
772 <p>In order to parse parameter entities, before starting the parse,
773 you must call <code><a href= "#XML_SetParamEntityParsing"
774 >XML_SetParamEntityParsing</a></code> with one of the following
775 arguments:</p>
776 <dl>
777 <dt><code>XML_PARAM_ENTITY_PARSING_NEVER</code></dt>
778 <dd>Don't parse parameter entities or the external subset</dd>
779 <dt><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></dt>
780 <dd>Parse parameter entities and the external subset unless
781 <code>standalone</code> was set to "yes" in the XML declaration.</dd>
782 <dt><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></dt>
783 <dd>Always parse parameter entities and the external subset</dd>
784 </dl>
785
786 <p>In order to read an external DTD, you also have to set an external
787 entity reference handler as described above.</p>
788
789 <h3 id="stop-resume">Temporarily Stopping Parsing</h3>
790
791 <p>Expat 1.95.8 introduces a new feature: its now possible to stop
792 parsing temporarily from within a handler function, even if more data
793 has already been passed into the parser. Applications for this
794 include</p>
795
796 <ul>
797 <li>Supporting the <a href= "http://www.w3.org/TR/xinclude/"
798 >XInclude</a> specification.</li>
799
800 <li>Delaying further processing until additional information is
801 available from some other source.</li>
802
803 <li>Adjusting processor load as task priorities shift within an
804 application.</li>
805
806 <li>Stopping parsing completely (simply free or reset the parser
807 instead of resuming in the outer parsing loop). This can be useful
808 if an application-domain error is found in the XML being parsed or if
809 the result of the parse is determined not to be useful after
810 all.</li>
811 </ul>
812
813 <p>To take advantage of this feature, the main parsing loop of an
814 application needs to support this specifically. It cannot be
815 supported with a parsing loop compatible with Expat 1.95.7 or
816 earlier (though existing loops will continue to work without
817 supporting the stop/resume feature).</p>
818
819 <p>An application that uses this feature for a single parser will have
820 the rough structure (in pseudo-code):</p>
821
822 <pre class="pseudocode">
823 fd = open_input()
824 p = create_parser()
825
826 if parse_xml(p, fd) {
827 /* suspended */
828
829 int suspended = 1;
830
831 while (suspended) {
832 do_something_else()
833 if ready_to_resume() {
834 suspended = continue_parsing(p, fd);
835 }
836 }
837 }
838 </pre>
839
840 <p>An application that may resume any of several parsers based on
841 input (either from the XML being parsed or some other source) will
842 certainly have more interesting control structures.</p>
843
844 <p>This C function could be used for the <code>parse_xml</code>
845 function mentioned in the pseudo-code above:</p>
846
847 <pre class="eg">
848 #define BUFF_SIZE 10240
849
850 /* Parse a document from the open file descriptor 'fd' until the parse
851 is complete (the document has been completely parsed, or there's
852 been an error), or the parse is stopped. Return non-zero when
853 the parse is merely suspended.
854 */
855 int
856 parse_xml(XML_Parser p, int fd)
857 {
858 for (;;) {
859 int last_chunk;
860 int bytes_read;
861 enum XML_Status status;
862
863 void *buff = XML_GetBuffer(p, BUFF_SIZE);
864 if (buff == NULL) {
865 /* handle error... */
866 return 0;
867 }
868 bytes_read = read(fd, buff, BUFF_SIZE);
869 if (bytes_read < 0) {
870 /* handle error... */
871 return 0;
872 }
873 status = XML_ParseBuffer(p, bytes_read, bytes_read == 0);
874 switch (status) {
875 case XML_STATUS_ERROR:
876 /* handle error... */
877 return 0;
878 case XML_STATUS_SUSPENDED:
879 return 1;
880 }
881 if (bytes_read == 0)
882 return 0;
883 }
884 }
885 </pre>
886
887 <p>The corresponding <code>continue_parsing</code> function is
888 somewhat simpler, since it only need deal with the return code from
889 <code><a href= "#XML_ResumeParser">XML_ResumeParser</a></code>; it can
890 delegate the input handling to the <code>parse_xml</code>
891 function:</p>
892
893 <pre class="eg">
894 /* Continue parsing a document which had been suspended. The 'p' and
895 'fd' arguments are the same as passed to parse_xml(). Return
896 non-zero when the parse is suspended.
897 */
898 int
899 continue_parsing(XML_Parser p, int fd)
900 {
901 enum XML_Status status = XML_ResumeParser(p);
902 switch (status) {
903 case XML_STATUS_ERROR:
904 /* handle error... */
905 return 0;
906 case XML_ERROR_NOT_SUSPENDED:
907 /* handle error... */
908 return 0;.
909 case XML_STATUS_SUSPENDED:
910 return 1;
911 }
912 return parse_xml(p, fd);
913 }
914 </pre>
915
916 <p>Now that we've seen what a mess the top-level parsing loop can
917 become, what have we gained? Very simply, we can now use the <code><a
918 href= "#XML_StopParser" >XML_StopParser</a></code> function to stop
919 parsing, without having to go to great lengths to avoid additional
920 processing that we're expecting to ignore. As a bonus, we get to stop
921 parsing <em>temporarily</em>, and come back to it when we're
922 ready.</p>
923
924 <p>To stop parsing from a handler function, use the <code><a href=
925 "#XML_StopParser" >XML_StopParser</a></code> function. This function
926 takes two arguments; the parser being stopped and a flag indicating
927 whether the parse can be resumed in the future.</p>
928
929 <!-- XXX really need more here -->
930
931
932 <hr />
933 <!-- ================================================================ -->
934
935 <h2><a name="reference">Expat Reference</a></h2>
936
937 <h3><a name="creation">Parser Creation</a></h3>
938
939 <h4 id="XML_ParserCreate">XML_ParserCreate</h4>
940 <pre class="fcndec">
941 XML_Parser XMLCALL
942 XML_ParserCreate(const XML_Char *encoding);
943 </pre>
944 <div class="fcndef">
945 Construct a new parser. If encoding is non-null, it specifies a
946 character encoding to use for the document. This overrides the document
947 encoding declaration. There are four built-in encodings:
948 <ul>
949 <li>US-ASCII</li>
950 <li>UTF-8</li>
951 <li>UTF-16</li>
952 <li>ISO-8859-1</li>
953 </ul>
954 Any other value will invoke a call to the UnknownEncodingHandler.
955 </div>
956
957 <h4 id="XML_ParserCreateNS">XML_ParserCreateNS</h4>
958 <pre class="fcndec">
959 XML_Parser XMLCALL
960 XML_ParserCreateNS(const XML_Char *encoding,
961 XML_Char sep);
962 </pre>
963 <div class="fcndef">
964 Constructs a new parser that has namespace processing in effect. Namespace
965 expanded element names and attribute names are returned as a concatenation
966 of the namespace URI, <em>sep</em>, and the local part of the name. This
967 means that you should pick a character for <em>sep</em> that can't be part
968 of an URI. Since Expat does not check namespace URIs for conformance, the
969 only safe choice for a namespace separator is a character that is illegal
970 in XML. For instance, <code>'\xFF'</code> is not legal in UTF-8, and
971 <code>'\xFFFF'</code> is not legal in UTF-16. There is a special case when
972 <em>sep</em> is the null character <code>'\0'</code>: the namespace URI and
973 the local part will be concatenated without any separator - this is intended
974 to support RDF processors. It is a programming error to use the null separator
975 with <a href= "#XML_SetReturnNSTriplet">namespace triplets</a>.</div>
976
977 <h4 id="XML_ParserCreate_MM">XML_ParserCreate_MM</h4>
978 <pre class="fcndec">
979 XML_Parser XMLCALL
980 XML_ParserCreate_MM(const XML_Char *encoding,
981 const XML_Memory_Handling_Suite *ms,
982 const XML_Char *sep);
983 </pre>
984 <pre class="signature">
985 typedef struct {
986 void *(XMLCALL *malloc_fcn)(size_t size);
987 void *(XMLCALL *realloc_fcn)(void *ptr, size_t size);
988 void (XMLCALL *free_fcn)(void *ptr);
989 } XML_Memory_Handling_Suite;
990 </pre>
991 <div class="fcndef">
992 <p>Construct a new parser using the suite of memory handling functions
993 specified in <code>ms</code>. If <code>ms</code> is NULL, then use the
994 standard set of memory management functions. If <code>sep</code> is
995 non NULL, then namespace processing is enabled in the created parser
996 and the character pointed at by sep is used as the separator between
997 the namespace URI and the local part of the name.</p>
998 </div>
999
1000 <h4 id="XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</h4>
1001 <pre class="fcndec">
1002 XML_Parser XMLCALL
1003 XML_ExternalEntityParserCreate(XML_Parser p,
1004 const XML_Char *context,
1005 const XML_Char *encoding);
1006 </pre>
1007 <div class="fcndef">
1008 Construct a new <code>XML_Parser</code> object for parsing an external
1009 general entity. Context is the context argument passed in a call to a
1010 ExternalEntityRefHandler. Other state information such as handlers,
1011 user data, namespace processing is inherited from the parser passed as
1012 the 1st argument. So you shouldn't need to call any of the behavior
1013 changing functions on this parser (unless you want it to act
1014 differently than the parent parser).
1015 </div>
1016
1017 <h4 id="XML_ParserFree">XML_ParserFree</h4>
1018 <pre class="fcndec">
1019 void XMLCALL
1020 XML_ParserFree(XML_Parser p);
1021 </pre>
1022 <div class="fcndef">
1023 Free memory used by the parser. Your application is responsible for
1024 freeing any memory associated with <a href="#userdata">user data</a>.
1025 </div>
1026
1027 <h4 id="XML_ParserReset">XML_ParserReset</h4>
1028 <pre class="fcndec">
1029 XML_Bool XMLCALL
1030 XML_ParserReset(XML_Parser p,
1031 const XML_Char *encoding);
1032 </pre>
1033 <div class="fcndef">
1034 Clean up the memory structures maintained by the parser so that it may
1035 be used again. After this has been called, <code>parser</code> is
1036 ready to start parsing a new document. All handlers are cleared from
1037 the parser, except for the unknownEncodingHandler. The parser's external
1038 state is re-initialized except for the values of ns and ns_triplets.
1039 This function may not be used on a parser created using <code><a href=
1040 "#XML_ExternalEntityParserCreate" >XML_ExternalEntityParserCreate</a
1041 ></code>; it will return <code>XML_FALSE</code> in that case. Returns
1042 <code>XML_TRUE</code> on success. Your application is responsible for
1043 dealing with any memory associated with <a href="#userdata">user data</a>.
1044 </div>
1045
1046 <h3><a name="parsing">Parsing</a></h3>
1047
1048 <p>To state the obvious: the three parsing functions <code><a href=
1049 "#XML_Parse" >XML_Parse</a></code>, <code><a href= "#XML_ParseBuffer">
1050 XML_ParseBuffer</a></code> and <code><a href= "#XML_GetBuffer">
1051 XML_GetBuffer</a></code> must not be called from within a handler
1052 unless they operate on a separate parser instance, that is, one that
1053 did not call the handler. For example, it is OK to call the parsing
1054 functions from within an <code>XML_ExternalEntityRefHandler</code>,
1055 if they apply to the parser created by
1056 <code><a href= "#XML_ExternalEntityParserCreate"
1057 >XML_ExternalEntityParserCreate</a></code>.</p>
1058
1059 <p>Note: The <code>len</code> argument passed to these functions
1060 should be considerably less than the maximum value for an integer,
1061 as it could create an integer overflow situation if the added
1062 lengths of a buffer and the unprocessed portion of the previous buffer
1063 exceed the maximum integer value. Input data at the end of a buffer
1064 will remain unprocessed if it is part of an XML token for which the
1065 end is not part of that buffer.</p>
1066
1067 <h4 id="XML_Parse">XML_Parse</h4>
1068 <pre class="fcndec">
1069 enum XML_Status XMLCALL
1070 XML_Parse(XML_Parser p,
1071 const char *s,
1072 int len,
1073 int isFinal);
1074 </pre>
1075 <pre class="signature">
1076 enum XML_Status {
1077 XML_STATUS_ERROR = 0,
1078 XML_STATUS_OK = 1
1079 };
1080 </pre>
1081 <div class="fcndef">
1082 Parse some more of the document. The string <code>s</code> is a buffer
1083 containing part (or perhaps all) of the document. The number of bytes of s
1084 that are part of the document is indicated by <code>len</code>. This means
1085 that <code>s</code> doesn't have to be null terminated. It also means that
1086 if <code>len</code> is larger than the number of bytes in the block of
1087 memory that <code>s</code> points at, then a memory fault is likely. The
1088 <code>isFinal</code> parameter informs the parser that this is the last
1089 piece of the document. Frequently, the last piece is empty (i.e.
1090 <code>len</code> is zero.)
1091 If a parse error occurred, it returns <code>XML_STATUS_ERROR</code>.
1092 Otherwise it returns <code>XML_STATUS_OK</code> value.
1093 </div>
1094
1095 <h4 id="XML_ParseBuffer">XML_ParseBuffer</h4>
1096 <pre class="fcndec">
1097 enum XML_Status XMLCALL
1098 XML_ParseBuffer(XML_Parser p,
1099 int len,
1100 int isFinal);
1101 </pre>
1102 <div class="fcndef">
1103 This is just like <code><a href= "#XML_Parse" >XML_Parse</a></code>,
1104 except in this case Expat provides the buffer. By obtaining the
1105 buffer from Expat with the <code><a href= "#XML_GetBuffer"
1106 >XML_GetBuffer</a></code> function, the application can avoid double
1107 copying of the input.
1108 </div>
1109
1110 <h4 id="XML_GetBuffer">XML_GetBuffer</h4>
1111 <pre class="fcndec">
1112 void * XMLCALL
1113 XML_GetBuffer(XML_Parser p,
1114 int len);
1115 </pre>
1116 <div class="fcndef">
1117 Obtain a buffer of size <code>len</code> to read a piece of the document
1118 into. A NULL value is returned if Expat can't allocate enough memory for
1119 this buffer. A NULL value may also be returned if <code>len</code> is zero.
1120 This has to be called prior to every call to
1121 <code><a href= "#XML_ParseBuffer" >XML_ParseBuffer</a></code>. A
1122 typical use would look like this:
1123
1124 <pre class="eg">
1125 for (;;) {
1126 int bytes_read;
1127 void *buff = XML_GetBuffer(p, BUFF_SIZE);
1128 if (buff == NULL) {
1129 /* handle error */
1130 }
1131
1132 bytes_read = read(docfd, buff, BUFF_SIZE);
1133 if (bytes_read < 0) {
1134 /* handle error */
1135 }
1136
1137 if (! XML_ParseBuffer(p, bytes_read, bytes_read == 0)) {
1138 /* handle parse error */
1139 }
1140
1141 if (bytes_read == 0)
1142 break;
1143 }
1144 </pre>
1145 </div>
1146
1147 <h4 id="XML_StopParser">XML_StopParser</h4>
1148 <pre class="fcndec">
1149 enum XML_Status XMLCALL
1150 XML_StopParser(XML_Parser p,
1151 XML_Bool resumable);
1152 </pre>
1153 <div class="fcndef">
1154
1155 <p>Stops parsing, causing <code><a href= "#XML_Parse"
1156 >XML_Parse</a></code> or <code><a href= "#XML_ParseBuffer"
1157 >XML_ParseBuffer</a></code> to return. Must be called from within a
1158 call-back handler, except when aborting (when <code>resumable</code>
1159 is <code>XML_FALSE</code>) an already suspended parser. Some
1160 call-backs may still follow because they would otherwise get
1161 lost, including</p>
1162 <ul>
1163 <li> the end element handler for empty elements when stopped in the
1164 start element handler,</li>
1165 <li> the end namespace declaration handler when stopped in the end
1166 element handler,</li>
1167 <li> the character data handler when stopped in the character data handler
1168 while making multiple call-backs on a contiguous chunk of characters,</li>
1169 </ul>
1170 <p>and possibly others.</p>
1171
1172 <p>This can be called from most handlers, including DTD related
1173 call-backs, except when parsing an external parameter entity and
1174 <code>resumable</code> is <code>XML_TRUE</code>. Returns
1175 <code>XML_STATUS_OK</code> when successful,
1176 <code>XML_STATUS_ERROR</code> otherwise. The possible error codes
1177 are:</p>
1178 <dl>
1179 <dt><code>XML_ERROR_SUSPENDED</code></dt>
1180 <dd>when suspending an already suspended parser.</dd>
1181 <dt><code>XML_ERROR_FINISHED</code></dt>
1182 <dd>when the parser has already finished.</dd>
1183 <dt><code>XML_ERROR_SUSPEND_PE</code></dt>
1184 <dd>when suspending while parsing an external PE.</dd>
1185 </dl>
1186
1187 <p>Since the stop/resume feature requires application support in the
1188 outer parsing loop, it is an error to call this function for a parser
1189 not being handled appropriately; see <a href= "#stop-resume"
1190 >Temporarily Stopping Parsing</a> for more information.</p>
1191
1192 <p>When <code>resumable</code> is <code>XML_TRUE</code> then parsing
1193 is <em>suspended</em>, that is, <code><a href= "#XML_Parse"
1194 >XML_Parse</a></code> and <code><a href= "#XML_ParseBuffer"
1195 >XML_ParseBuffer</a></code> return <code>XML_STATUS_SUSPENDED</code>.
1196 Otherwise, parsing is <em>aborted</em>, that is, <code><a href=
1197 "#XML_Parse" >XML_Parse</a></code> and <code><a href=
1198 "#XML_ParseBuffer" >XML_ParseBuffer</a></code> return
1199 <code>XML_STATUS_ERROR</code> with error code
1200 <code>XML_ERROR_ABORTED</code>.</p>
1201
1202 <p><strong>Note:</strong>
1203 This will be applied to the current parser instance only, that is, if
1204 there is a parent parser then it will continue parsing when the
1205 external entity reference handler returns. It is up to the
1206 implementation of that handler to call <code><a href=
1207 "#XML_StopParser" >XML_StopParser</a></code> on the parent parser
1208 (recursively), if one wants to stop parsing altogether.</p>
1209
1210 <p>When suspended, parsing can be resumed by calling <code><a href=
1211 "#XML_ResumeParser" >XML_ResumeParser</a></code>.</p>
1212
1213 <p>New in Expat 1.95.8.</p>
1214 </div>
1215
1216 <h4 id="XML_ResumeParser">XML_ResumeParser</h4>
1217 <pre class="fcndec">
1218 enum XML_Status XMLCALL
1219 XML_ResumeParser(XML_Parser p);
1220 </pre>
1221 <div class="fcndef">
1222 <p>Resumes parsing after it has been suspended with <code><a href=
1223 "#XML_StopParser" >XML_StopParser</a></code>. Must not be called from
1224 within a handler call-back. Returns same status codes as <code><a
1225 href= "#XML_Parse">XML_Parse</a></code> or <code><a href=
1226 "#XML_ParseBuffer" >XML_ParseBuffer</a></code>. An additional error
1227 code, <code>XML_ERROR_NOT_SUSPENDED</code>, will be returned if the
1228 parser was not currently suspended.</p>
1229
1230 <p><strong>Note:</strong>
1231 This must be called on the most deeply nested child parser instance
1232 first, and on its parent parser only after the child parser has
1233 finished, to be applied recursively until the document entity's parser
1234 is restarted. That is, the parent parser will not resume by itself
1235 and it is up to the application to call <code><a href=
1236 "#XML_ResumeParser" >XML_ResumeParser</a></code> on it at the
1237 appropriate moment.</p>
1238
1239 <p>New in Expat 1.95.8.</p>
1240 </div>
1241
1242 <h4 id="XML_GetParsingStatus">XML_GetParsingStatus</h4>
1243 <pre class="fcndec">
1244 void XMLCALL
1245 XML_GetParsingStatus(XML_Parser p,
1246 XML_ParsingStatus *status);
1247 </pre>
1248 <pre class="signature">
1249 enum XML_Parsing {
1250 XML_INITIALIZED,
1251 XML_PARSING,
1252 XML_FINISHED,
1253 XML_SUSPENDED
1254 };
1255
1256 typedef struct {
1257 enum XML_Parsing parsing;
1258 XML_Bool finalBuffer;
1259 } XML_ParsingStatus;
1260 </pre>
1261 <div class="fcndef">
1262 <p>Returns status of parser with respect to being initialized,
1263 parsing, finished, or suspended, and whether the final buffer is being
1264 processed. The <code>status</code> parameter <em>must not</em> be
1265 NULL.</p>
1266
1267 <p>New in Expat 1.95.8.</p>
1268 </div>
1269
1270
1271 <h3><a name="setting">Handler Setting</a></h3>
1272
1273 <p>Although handlers are typically set prior to parsing and left alone, an
1274 application may choose to set or change the handler for a parsing event
1275 while the parse is in progress. For instance, your application may choose
1276 to ignore all text not descended from a <code>para</code> element. One
1277 way it could do this is to set the character handler when a para start tag
1278 is seen, and unset it for the corresponding end tag.</p>
1279
1280 <p>A handler may be <em>unset</em> by providing a NULL pointer to the
1281 appropriate handler setter. None of the handler setting functions have
1282 a return value.</p>
1283
1284 <p>Your handlers will be receiving strings in arrays of type
1285 <code>XML_Char</code>. This type is conditionally defined in expat.h as
1286 either <code>char</code>, <code>wchar_t</code> or <code>unsigned short</code>.
1287 The former implies UTF-8 encoding, the latter two imply UTF-16 encoding.
1288 Note that you'll receive them in this form independent of the original
1289 encoding of the document.</p>
1290
1291 <div class="handler">
1292 <h4 id="XML_SetStartElementHandler">XML_SetStartElementHandler</h4>
1293 <pre class="setter">
1294 void XMLCALL
1295 XML_SetStartElementHandler(XML_Parser p,
1296 XML_StartElementHandler start);
1297 </pre>
1298 <pre class="signature">
1299 typedef void
1300 (XMLCALL *XML_StartElementHandler)(void *userData,
1301 const XML_Char *name,
1302 const XML_Char **atts);
1303 </pre>
1304 <p>Set handler for start (and empty) tags. Attributes are passed to the start
1305 handler as a pointer to a vector of char pointers. Each attribute seen in
1306 a start (or empty) tag occupies 2 consecutive places in this vector: the
1307 attribute name followed by the attribute value. These pairs are terminated
1308 by a null pointer.</p>
1309 <p>Note that an empty tag generates a call to both start and end handlers
1310 (in that order).</p>
1311 </div>
1312
1313 <div class="handler">
1314 <h4 id="XML_SetEndElementHandler">XML_SetEndElementHandler</h4>
1315 <pre class="setter">
1316 void XMLCALL
1317 XML_SetEndElementHandler(XML_Parser p,
1318 XML_EndElementHandler);
1319 </pre>
1320 <pre class="signature">
1321 typedef void
1322 (XMLCALL *XML_EndElementHandler)(void *userData,
1323 const XML_Char *name);
1324 </pre>
1325 <p>Set handler for end (and empty) tags. As noted above, an empty tag
1326 generates a call to both start and end handlers.</p>
1327 </div>
1328
1329 <div class="handler">
1330 <h4 id="XML_SetElementHandler">XML_SetElementHandler</h4>
1331 <pre class="setter">
1332 void XMLCALL
1333 XML_SetElementHandler(XML_Parser p,
1334 XML_StartElementHandler start,
1335 XML_EndElementHandler end);
1336 </pre>
1337 <p>Set handlers for start and end tags with one call.</p>
1338 </div>
1339
1340 <div class="handler">
1341 <h4 id="XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</h4>
1342 <pre class="setter">
1343 void XMLCALL
1344 XML_SetCharacterDataHandler(XML_Parser p,
1345 XML_CharacterDataHandler charhndl)
1346 </pre>
1347 <pre class="signature">
1348 typedef void
1349 (XMLCALL *XML_CharacterDataHandler)(void *userData,
1350 const XML_Char *s,
1351 int len);
1352 </pre>
1353 <p>Set a text handler. The string your handler receives
1354 is <em>NOT null-terminated</em>. You have to use the length argument
1355 to deal with the end of the string. A single block of contiguous text
1356 free of markup may still result in a sequence of calls to this handler.
1357 In other words, if you're searching for a pattern in the text, it may
1358 be split across calls to this handler. Note: Setting this handler to NULL
1359 may <em>NOT immediately</em> terminate call-backs if the parser is currently
1360 processing such a single block of contiguous markup-free text, as the parser
1361 will continue calling back until the end of the block is reached.</p>
1362 </div>
1363
1364 <div class="handler">
1365 <h4 id="XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</h4>
1366 <pre class="setter">
1367 void XMLCALL
1368 XML_SetProcessingInstructionHandler(XML_Parser p,
1369 XML_ProcessingInstructionHandler proc)
1370 </pre>
1371 <pre class="signature">
1372 typedef void
1373 (XMLCALL *XML_ProcessingInstructionHandler)(void *userData,
1374 const XML_Char *target,
1375 const XML_Char *data);
1376
1377 </pre>
1378 <p>Set a handler for processing instructions. The target is the first word
1379 in the processing instruction. The data is the rest of the characters in
1380 it after skipping all whitespace after the initial word.</p>
1381 </div>
1382
1383 <div class="handler">
1384 <h4 id="XML_SetCommentHandler">XML_SetCommentHandler</h4>
1385 <pre class="setter">
1386 void XMLCALL
1387 XML_SetCommentHandler(XML_Parser p,
1388 XML_CommentHandler cmnt)
1389 </pre>
1390 <pre class="signature">
1391 typedef void
1392 (XMLCALL *XML_CommentHandler)(void *userData,
1393 const XML_Char *data);
1394 </pre>
1395 <p>Set a handler for comments. The data is all text inside the comment
1396 delimiters.</p>
1397 </div>
1398
1399 <div class="handler">
1400 <h4 id="XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</h4>
1401 <pre class="setter">
1402 void XMLCALL
1403 XML_SetStartCdataSectionHandler(XML_Parser p,
1404 XML_StartCdataSectionHandler start);
1405 </pre>
1406 <pre class="signature">
1407 typedef void
1408 (XMLCALL *XML_StartCdataSectionHandler)(void *userData);
1409 </pre>
1410 <p>Set a handler that gets called at the beginning of a CDATA section.</p>
1411 </div>
1412
1413 <div class="handler">
1414 <h4 id="XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</h4>
1415 <pre class="setter">
1416 void XMLCALL
1417 XML_SetEndCdataSectionHandler(XML_Parser p,
1418 XML_EndCdataSectionHandler end);
1419 </pre>
1420 <pre class="signature">
1421 typedef void
1422 (XMLCALL *XML_EndCdataSectionHandler)(void *userData);
1423 </pre>
1424 <p>Set a handler that gets called at the end of a CDATA section.</p>
1425 </div>
1426
1427 <div class="handler">
1428 <h4 id="XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</h4>
1429 <pre class="setter">
1430 void XMLCALL
1431 XML_SetCdataSectionHandler(XML_Parser p,
1432 XML_StartCdataSectionHandler start,
1433 XML_EndCdataSectionHandler end)
1434 </pre>
1435 <p>Sets both CDATA section handlers with one call.</p>
1436 </div>
1437
1438 <div class="handler">
1439 <h4 id="XML_SetDefaultHandler">XML_SetDefaultHandler</h4>
1440 <pre class="setter">
1441 void XMLCALL
1442 XML_SetDefaultHandler(XML_Parser p,
1443 XML_DefaultHandler hndl)
1444 </pre>
1445 <pre class="signature">
1446 typedef void
1447 (XMLCALL *XML_DefaultHandler)(void *userData,
1448 const XML_Char *s,
1449 int len);
1450 </pre>
1451
1452 <p>Sets a handler for any characters in the document which wouldn't
1453 otherwise be handled. This includes both data for which no handlers
1454 can be set (like some kinds of DTD declarations) and data which could
1455 be reported but which currently has no handler set. The characters
1456 are passed exactly as they were present in the XML document except
1457 that they will be encoded in UTF-8 or UTF-16. Line boundaries are not
1458 normalized. Note that a byte order mark character is not passed to the
1459 default handler. There are no guarantees about how characters are
1460 divided between calls to the default handler: for example, a comment
1461 might be split between multiple calls. Setting the handler with
1462 this call has the side effect of turning off expansion of references
1463 to internally defined general entities. Instead these references are
1464 passed to the default handler.</p>
1465
1466 <p>See also <code><a
1467 href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>.</p>
1468 </div>
1469
1470 <div class="handler">
1471 <h4 id="XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</h4>
1472 <pre class="setter">
1473 void XMLCALL
1474 XML_SetDefaultHandlerExpand(XML_Parser p,
1475 XML_DefaultHandler hndl)
1476 </pre>
1477 <pre class="signature">
1478 typedef void
1479 (XMLCALL *XML_DefaultHandler)(void *userData,
1480 const XML_Char *s,
1481 int len);
1482 </pre>
1483 <p>This sets a default handler, but doesn't inhibit the expansion of
1484 internal entity references. The entity reference will not be passed
1485 to the default handler.</p>
1486
1487 <p>See also <code><a
1488 href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>.</p>
1489 </div>
1490
1491 <div class="handler">
1492 <h4 id="XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</h4>
1493 <pre class="setter">
1494 void XMLCALL
1495 XML_SetExternalEntityRefHandler(XML_Parser p,
1496 XML_ExternalEntityRefHandler hndl)
1497 </pre>
1498 <pre class="signature">
1499 typedef int
1500 (XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser p,
1501 const XML_Char *context,
1502 const XML_Char *base,
1503 const XML_Char *systemId,
1504 const XML_Char *publicId);
1505 </pre>
1506 <p>Set an external entity reference handler. This handler is also
1507 called for processing an external DTD subset if parameter entity parsing
1508 is in effect. (See <a href="#XML_SetParamEntityParsing">
1509 <code>XML_SetParamEntityParsing</code></a>.)</p>
1510
1511 <p>The <code>context</code> parameter specifies the parsing context in
1512 the format expected by the <code>context</code> argument to <code><a
1513 href="#XML_ExternalEntityParserCreate"
1514 >XML_ExternalEntityParserCreate</a></code>. <code>code</code> is
1515 valid only until the handler returns, so if the referenced entity is
1516 to be parsed later, it must be copied. <code>context</code> is NULL
1517 only when the entity is a parameter entity, which is how one can
1518 differentiate between general and parameter entities.</p>
1519
1520 <p>The <code>base</code> parameter is the base to use for relative
1521 system identifiers. It is set by <code><a
1522 href="#XML_SetBase">XML_SetBase</a></code> and may be NULL. The
1523 <code>publicId</code> parameter is the public id given in the entity
1524 declaration and may be NULL. <code>systemId</code> is the system
1525 identifier specified in the entity declaration and is never NULL.</p>
1526
1527 <p>There are a couple of ways in which this handler differs from
1528 others. First, this handler returns a status indicator (an
1529 integer). <code>XML_STATUS_OK</code> should be returned for successful
1530 handling of the external entity reference. Returning
1531 <code>XML_STATUS_ERROR</code> indicates failure, and causes the
1532 calling parser to return an
1533 <code>XML_ERROR_EXTERNAL_ENTITY_HANDLING</code> error.</p>
1534
1535 <p>Second, instead of having the user data as its first argument, it
1536 receives the parser that encountered the entity reference. This, along
1537 with the context parameter, may be used as arguments to a call to
1538 <code><a href= "#XML_ExternalEntityParserCreate"
1539 >XML_ExternalEntityParserCreate</a></code>. Using the returned
1540 parser, the body of the external entity can be recursively parsed.</p>
1541
1542 <p>Since this handler may be called recursively, it should not be saving
1543 information into global or static variables.</p>
1544 </div>
1545
1546 <h4 id="XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</h4>
1547 <pre class="fcndec">
1548 void XMLCALL
1549 XML_SetExternalEntityRefHandlerArg(XML_Parser p,
1550 void *arg)
1551 </pre>
1552 <div class="fcndef">
1553 <p>Set the argument passed to the ExternalEntityRefHandler. If
1554 <code>arg</code> is not NULL, it is the new value passed to the
1555 handler set using <code><a href="#XML_SetExternalEntityRefHandler"
1556 >XML_SetExternalEntityRefHandler</a></code>; if <code>arg</code> is
1557 NULL, the argument passed to the handler function will be the parser
1558 object itself.</p>
1559
1560 <p><strong>Note:</strong>
1561 The type of <code>arg</code> and the type of the first argument to the
1562 ExternalEntityRefHandler do not match. This function takes a
1563 <code>void *</code> to be passed to the handler, while the handler
1564 accepts an <code>XML_Parser</code>. This is a historical accident,
1565 but will not be corrected before Expat 2.0 (at the earliest) to avoid
1566 causing compiler warnings for code that's known to work with this
1567 API. It is the responsibility of the application code to know the
1568 actual type of the argument passed to the handler and to manage it
1569 properly.</p>
1570 </div>
1571
1572 <div class="handler">
1573 <h4 id="XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</h4>
1574 <pre class="setter">
1575 void XMLCALL
1576 XML_SetSkippedEntityHandler(XML_Parser p,
1577 XML_SkippedEntityHandler handler)
1578 </pre>
1579 <pre class="signature">
1580 typedef void
1581 (XMLCALL *XML_SkippedEntityHandler)(void *userData,
1582 const XML_Char *entityName,
1583 int is_parameter_entity);
1584 </pre>
1585 <p>Set a skipped entity handler. This is called in two situations:</p>
1586 <ol>
1587 <li>An entity reference is encountered for which no declaration
1588 has been read <em>and</em> this is not an error.</li>
1589 <li>An internal entity reference is read, but not expanded, because
1590 <a href="#XML_SetDefaultHandler"><code>XML_SetDefaultHandler</code></a>
1591 has been called.</li>
1592 </ol>
1593 <p>The <code>is_parameter_entity</code> argument will be non-zero for
1594 a parameter entity and zero for a general entity.</p> <p>Note: Skipped
1595 parameter entities in declarations and skipped general entities in
1596 attribute values cannot be reported, because the event would be out of
1597 sync with the reporting of the declarations or attribute values</p>
1598 </div>
1599
1600 <div class="handler">
1601 <h4 id="XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</h4>
1602 <pre class="setter">
1603 void XMLCALL
1604 XML_SetUnknownEncodingHandler(XML_Parser p,
1605 XML_UnknownEncodingHandler enchandler,
1606 void *encodingHandlerData)
1607 </pre>
1608 <pre class="signature">
1609 typedef int
1610 (XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData,
1611 const XML_Char *name,
1612 XML_Encoding *info);
1613
1614 typedef struct {
1615 int map[256];
1616 void *data;
1617 int (XMLCALL *convert)(void *data, const char *s);
1618 void (XMLCALL *release)(void *data);
1619 } XML_Encoding;
1620 </pre>
1621 <p>Set a handler to deal with encodings other than the <a
1622 href="#builtin_encodings">built in set</a>. This should be done before
1623 <code><a href= "#XML_Parse" >XML_Parse</a></code> or <code><a href=
1624 "#XML_ParseBuffer" >XML_ParseBuffer</a></code> have been called on the
1625 given parser.</p> <p>If the handler knows how to deal with an encoding
1626 with the given name, it should fill in the <code>info</code> data
1627 structure and return <code>XML_STATUS_OK</code>. Otherwise it
1628 should return <code>XML_STATUS_ERROR</code>. The handler will be called
1629 at most once per parsed (external) entity. The optional application
1630 data pointer <code>encodingHandlerData</code> will be passed back to
1631 the handler.</p>
1632
1633 <p>The map array contains information for every possible leading
1634 byte in a byte sequence. If the corresponding value is >= 0, then it's
1635 a single byte sequence and the byte encodes that Unicode value. If the
1636 value is -1, then that byte is invalid as the initial byte in a sequence.
1637 If the value is -n, where n is an integer > 1, then n is the number of
1638 bytes in the sequence and the actual conversion is accomplished by a
1639 call to the function pointed at by convert. This function may return -1
1640 if the sequence itself is invalid. The convert pointer may be null if
1641 there are only single byte codes. The data parameter passed to the convert
1642 function is the data pointer from <code>XML_Encoding</code>. The
1643 string s is <em>NOT</em> null-terminated and points at the sequence of
1644 bytes to be converted.</p>
1645
1646 <p>The function pointed at by <code>release</code> is called by the
1647 parser when it is finished with the encoding. It may be NULL.</p>
1648 </div>
1649
1650 <div class="handler">
1651 <h4 id="XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</h4>
1652 <pre class="setter">
1653 void XMLCALL
1654 XML_SetStartNamespaceDeclHandler(XML_Parser p,
1655 XML_StartNamespaceDeclHandler start);
1656 </pre>
1657 <pre class="signature">
1658 typedef void
1659 (XMLCALL *XML_StartNamespaceDeclHandler)(void *userData,
1660 const XML_Char *prefix,
1661 const XML_Char *uri);
1662 </pre>
1663 <p>Set a handler to be called when a namespace is declared. Namespace
1664 declarations occur inside start tags. But the namespace declaration start
1665 handler is called before the start tag handler for each namespace declared
1666 in that start tag.</p>
1667 </div>
1668
1669 <div class="handler">
1670 <h4 id="XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</h4>
1671 <pre class="setter">
1672 void XMLCALL
1673 XML_SetEndNamespaceDeclHandler(XML_Parser p,
1674 XML_EndNamespaceDeclHandler end);
1675 </pre>
1676 <pre class="signature">
1677 typedef void
1678 (XMLCALL *XML_EndNamespaceDeclHandler)(void *userData,
1679 const XML_Char *prefix);
1680 </pre>
1681 <p>Set a handler to be called when leaving the scope of a namespace
1682 declaration. This will be called, for each namespace declaration,
1683 after the handler for the end tag of the element in which the
1684 namespace was declared.</p>
1685 </div>
1686
1687 <div class="handler">
1688 <h4 id="XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</h4>
1689 <pre class="setter">
1690 void XMLCALL
1691 XML_SetNamespaceDeclHandler(XML_Parser p,
1692 XML_StartNamespaceDeclHandler start,
1693 XML_EndNamespaceDeclHandler end)
1694 </pre>
1695 <p>Sets both namespace declaration handlers with a single call.</p>
1696 </div>
1697
1698 <div class="handler">
1699 <h4 id="XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</h4>
1700 <pre class="setter">
1701 void XMLCALL
1702 XML_SetXmlDeclHandler(XML_Parser p,
1703 XML_XmlDeclHandler xmldecl);
1704 </pre>
1705 <pre class="signature">
1706 typedef void
1707 (XMLCALL *XML_XmlDeclHandler)(void *userData,
1708 const XML_Char *version,
1709 const XML_Char *encoding,
1710 int standalone);
1711 </pre>
1712 <p>Sets a handler that is called for XML declarations and also for
1713 text declarations discovered in external entities. The way to
1714 distinguish is that the <code>version</code> parameter will be NULL
1715 for text declarations. The <code>encoding</code> parameter may be NULL
1716 for an XML declaration. The <code>standalone</code> argument will
1717 contain -1, 0, or 1 indicating respectively that there was no
1718 standalone parameter in the declaration, that it was given as no, or
1719 that it was given as yes.</p>
1720 </div>
1721
1722 <div class="handler">
1723 <h4 id="XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</h4>
1724 <pre class="setter">
1725 void XMLCALL
1726 XML_SetStartDoctypeDeclHandler(XML_Parser p,
1727 XML_StartDoctypeDeclHandler start);
1728 </pre>
1729 <pre class="signature">
1730 typedef void
1731 (XMLCALL *XML_StartDoctypeDeclHandler)(void *userData,
1732 const XML_Char *doctypeName,
1733 const XML_Char *sysid,
1734 const XML_Char *pubid,
1735 int has_internal_subset);
1736 </pre>
1737 <p>Set a handler that is called at the start of a DOCTYPE declaration,
1738 before any external or internal subset is parsed. Both <code>sysid</code>
1739 and <code>pubid</code> may be NULL. The <code>has_internal_subset</code>
1740 will be non-zero if the DOCTYPE declaration has an internal subset.</p>
1741 </div>
1742
1743 <div class="handler">
1744 <h4 id="XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</h4>
1745 <pre class="setter">
1746 void XMLCALL
1747 XML_SetEndDoctypeDeclHandler(XML_Parser p,
1748 XML_EndDoctypeDeclHandler end);
1749 </pre>
1750 <pre class="signature">
1751 typedef void
1752 (XMLCALL *XML_EndDoctypeDeclHandler)(void *userData);
1753 </pre>
1754 <p>Set a handler that is called at the end of a DOCTYPE declaration,
1755 after parsing any external subset.</p>
1756 </div>
1757
1758 <div class="handler">
1759 <h4 id="XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</h4>
1760 <pre class="setter">
1761 void XMLCALL
1762 XML_SetDoctypeDeclHandler(XML_Parser p,
1763 XML_StartDoctypeDeclHandler start,
1764 XML_EndDoctypeDeclHandler end);
1765 </pre>
1766 <p>Set both doctype handlers with one call.</p>
1767 </div>
1768
1769 <div class="handler">
1770 <h4 id="XML_SetElementDeclHandler">XML_SetElementDeclHandler</h4>
1771 <pre class="setter">
1772 void XMLCALL
1773 XML_SetElementDeclHandler(XML_Parser p,
1774 XML_ElementDeclHandler eldecl);
1775 </pre>
1776 <pre class="signature">
1777 typedef void
1778 (XMLCALL *XML_ElementDeclHandler)(void *userData,
1779 const XML_Char *name,
1780 XML_Content *model);
1781 </pre>
1782 <pre class="signature">
1783 enum XML_Content_Type {
1784 XML_CTYPE_EMPTY = 1,
1785 XML_CTYPE_ANY,
1786 XML_CTYPE_MIXED,
1787 XML_CTYPE_NAME,
1788 XML_CTYPE_CHOICE,
1789 XML_CTYPE_SEQ
1790 };
1791
1792 enum XML_Content_Quant {
1793 XML_CQUANT_NONE,
1794 XML_CQUANT_OPT,
1795 XML_CQUANT_REP,
1796 XML_CQUANT_PLUS
1797 };
1798
1799 typedef struct XML_cp XML_Content;
1800
1801 struct XML_cp {
1802 enum XML_Content_Type type;
1803 enum XML_Content_Quant quant;
1804 const XML_Char * name;
1805 unsigned int numchildren;
1806 XML_Content * children;
1807 };
1808 </pre>
1809 <p>Sets a handler for element declarations in a DTD. The handler gets
1810 called with the name of the element in the declaration and a pointer
1811 to a structure that contains the element model. It is the
1812 application's responsibility to free this data structure using
1813 <code><a href="#XML_FreeContentModel"
1814 >XML_FreeContentModel</a></code>.</p>
1815
1816 <p>The <code>model</code> argument is the root of a tree of
1817 <code>XML_Content</code> nodes. If <code>type</code> equals
1818 <code>XML_CTYPE_EMPTY</code> or <code>XML_CTYPE_ANY</code>, then
1819 <code>quant</code> will be <code>XML_CQUANT_NONE</code>, and the other
1820 fields will be zero or NULL. If <code>type</code> is
1821 <code>XML_CTYPE_MIXED</code>, then <code>quant</code> will be
1822 <code>XML_CQUANT_NONE</code> or <code>XML_CQUANT_REP</code> and
1823 <code>numchildren</code> will contain the number of elements that are
1824 allowed to be mixed in and <code>children</code> points to an array of
1825 <code>XML_Content</code> structures that will all have type
1826 XML_CTYPE_NAME with no quantification. Only the root node can be type
1827 <code>XML_CTYPE_EMPTY</code>, <code>XML_CTYPE_ANY</code>, or
1828 <code>XML_CTYPE_MIXED</code>.</p>
1829
1830 <p>For type <code>XML_CTYPE_NAME</code>, the <code>name</code> field
1831 points to the name and the <code>numchildren</code> and
1832 <code>children</code> fields will be zero and NULL. The
1833 <code>quant</code> field will indicate any quantifiers placed on the
1834 name.</p>
1835
1836 <p>Types <code>XML_CTYPE_CHOICE</code> and <code>XML_CTYPE_SEQ</code>
1837 indicate a choice or sequence respectively. The
1838 <code>numchildren</code> field indicates how many nodes in the choice
1839 or sequence and <code>children</code> points to the nodes.</p>
1840 </div>
1841
1842 <div class="handler">
1843 <h4 id="XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</h4>
1844 <pre class="setter">
1845 void XMLCALL
1846 XML_SetAttlistDeclHandler(XML_Parser p,
1847 XML_AttlistDeclHandler attdecl);
1848 </pre>
1849 <pre class="signature">
1850 typedef void
1851 (XMLCALL *XML_AttlistDeclHandler)(void *userData,
1852 const XML_Char *elname,
1853 const XML_Char *attname,
1854 const XML_Char *att_type,
1855 const XML_Char *dflt,
1856 int isrequired);
1857 </pre>
1858 <p>Set a handler for attlist declarations in the DTD. This handler is
1859 called for <em>each</em> attribute. So a single attlist declaration
1860 with multiple attributes declared will generate multiple calls to this
1861 handler. The <code>elname</code> parameter returns the name of the
1862 element for which the attribute is being declared. The attribute name
1863 is in the <code>attname</code> parameter. The attribute type is in the
1864 <code>att_type</code> parameter. It is the string representing the
1865 type in the declaration with whitespace removed.</p>
1866
1867 <p>The <code>dflt</code> parameter holds the default value. It will be
1868 NULL in the case of "#IMPLIED" or "#REQUIRED" attributes. You can
1869 distinguish these two cases by checking the <code>isrequired</code>
1870 parameter, which will be true in the case of "#REQUIRED" attributes.
1871 Attributes which are "#FIXED" will have also have a true
1872 <code>isrequired</code>, but they will have the non-NULL fixed value
1873 in the <code>dflt</code> parameter.</p>
1874 </div>
1875
1876 <div class="handler">
1877 <h4 id="XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</h4>
1878 <pre class="setter">
1879 void XMLCALL
1880 XML_SetEntityDeclHandler(XML_Parser p,
1881 XML_EntityDeclHandler handler);
1882 </pre>
1883 <pre class="signature">
1884 typedef void
1885 (XMLCALL *XML_EntityDeclHandler)(void *userData,
1886 const XML_Char *entityName,
1887 int is_parameter_entity,
1888 const XML_Char *value,
1889 int value_length,
1890 const XML_Char *base,
1891 const XML_Char *systemId,
1892 const XML_Char *publicId,
1893 const XML_Char *notationName);
1894 </pre>
1895 <p>Sets a handler that will be called for all entity declarations.
1896 The <code>is_parameter_entity</code> argument will be non-zero in the
1897 case of parameter entities and zero otherwise.</p>
1898
1899 <p>For internal entities (<code><!ENTITY foo "bar"></code>),
1900 <code>value</code> will be non-NULL and <code>systemId</code>,
1901 <code>publicId</code>, and <code>notationName</code> will all be NULL.
1902 The value string is <em>not</em> NULL terminated; the length is
1903 provided in the <code>value_length</code> parameter. Do not use
1904 <code>value_length</code> to test for internal entities, since it is
1905 legal to have zero-length values. Instead check for whether or not
1906 <code>value</code> is NULL.</p> <p>The <code>notationName</code>
1907 argument will have a non-NULL value only for unparsed entity
1908 declarations.</p>
1909 </div>
1910
1911 <div class="handler">
1912 <h4 id="XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</h4>
1913 <pre class="setter">
1914 void XMLCALL
1915 XML_SetUnparsedEntityDeclHandler(XML_Parser p,
1916 XML_UnparsedEntityDeclHandler h)
1917 </pre>
1918 <pre class="signature">
1919 typedef void
1920 (XMLCALL *XML_UnparsedEntityDeclHandler)(void *userData,
1921 const XML_Char *entityName,
1922 const XML_Char *base,
1923 const XML_Char *systemId,
1924 const XML_Char *publicId,
1925 const XML_Char *notationName);
1926 </pre>
1927 <p>Set a handler that receives declarations of unparsed entities. These
1928 are entity declarations that have a notation (NDATA) field:</p>
1929
1930 <div id="eg"><pre>
1931 <!ENTITY logo SYSTEM "images/logo.gif" NDATA gif>
1932 </pre></div>
1933 <p>This handler is obsolete and is provided for backwards
1934 compatibility. Use instead <a href= "#XML_SetEntityDeclHandler"
1935 >XML_SetEntityDeclHandler</a>.</p>
1936 </div>
1937
1938 <div class="handler">
1939 <h4 id="XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</h4>
1940 <pre class="setter">
1941 void XMLCALL
1942 XML_SetNotationDeclHandler(XML_Parser p,
1943 XML_NotationDeclHandler h)
1944 </pre>
1945 <pre class="signature">
1946 typedef void
1947 (XMLCALL *XML_NotationDeclHandler)(void *userData,
1948 const XML_Char *notationName,
1949 const XML_Char *base,
1950 const XML_Char *systemId,
1951 const XML_Char *publicId);
1952 </pre>
1953 <p>Set a handler that receives notation declarations.</p>
1954 </div>
1955
1956 <div class="handler">
1957 <h4 id="XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</h4>
1958 <pre class="setter">
1959 void XMLCALL
1960 XML_SetNotStandaloneHandler(XML_Parser p,
1961 XML_NotStandaloneHandler h)
1962 </pre>
1963 <pre class="signature">
1964 typedef int
1965 (XMLCALL *XML_NotStandaloneHandler)(void *userData);
1966 </pre>
1967 <p>Set a handler that is called if the document is not "standalone".
1968 This happens when there is an external subset or a reference to a
1969 parameter entity, but does not have standalone set to "yes" in an XML
1970 declaration. If this handler returns <code>XML_STATUS_ERROR</code>,
1971 then the parser will throw an <code>XML_ERROR_NOT_STANDALONE</code>
1972 error.</p>
1973 </div>
1974
1975 <h3><a name="position">Parse position and error reporting functions</a></h3>
1976
1977 <p>These are the functions you'll want to call when the parse
1978 functions return <code>XML_STATUS_ERROR</code> (a parse error has
1979 occurred), although the position reporting functions are useful outside
1980 of errors. The position reported is the byte position (in the original
1981 document or entity encoding) of the first of the sequence of
1982 characters that generated the current event (or the error that caused
1983 the parse functions to return <code>XML_STATUS_ERROR</code>.) The
1984 exceptions are callbacks triggered by declarations in the document
1985 prologue, in which case they exact position reported is somewhere in the
1986 relevant markup, but not necessarily as meaningful as for other
1987 events.</p>
1988
1989 <p>The position reporting functions are accurate only outside of the
1990 DTD. In other words, they usually return bogus information when
1991 called from within a DTD declaration handler.</p>
1992
1993 <h4 id="XML_GetErrorCode">XML_GetErrorCode</h4>
1994 <pre class="fcndec">
1995 enum XML_Error XMLCALL
1996 XML_GetErrorCode(XML_Parser p);
1997 </pre>
1998 <div class="fcndef">
1999 Return what type of error has occurred.
2000 </div>
2001
2002 <h4 id="XML_ErrorString">XML_ErrorString</h4>
2003 <pre class="fcndec">
2004 const XML_LChar * XMLCALL
2005 XML_ErrorString(enum XML_Error code);
2006 </pre>
2007 <div class="fcndef">
2008 Return a string describing the error corresponding to code.
2009 The code should be one of the enums that can be returned from
2010 <code><a href= "#XML_GetErrorCode" >XML_GetErrorCode</a></code>.
2011 </div>
2012
2013 <h4 id="XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</h4>
2014 <pre class="fcndec">
2015 XML_Index XMLCALL
2016 XML_GetCurrentByteIndex(XML_Parser p);
2017 </pre>
2018 <div class="fcndef">
2019 Return the byte offset of the position. This always corresponds to
2020 the values returned by <code><a href= "#XML_GetCurrentLineNumber"
2021 >XML_GetCurrentLineNumber</a></code> and <code><a href=
2022 "#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber</a></code>.
2023 </div>
2024
2025 <h4 id="XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</h4>
2026 <pre class="fcndec">
2027 XML_Size XMLCALL
2028 XML_GetCurrentLineNumber(XML_Parser p);
2029 </pre>
2030 <div class="fcndef">
2031 Return the line number of the position. The first line is reported as
2032 <code>1</code>.
2033 </div>
2034
2035 <h4 id="XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</h4>
2036 <pre class="fcndec">
2037 XML_Size XMLCALL
2038 XML_GetCurrentColumnNumber(XML_Parser p);
2039 </pre>
2040 <div class="fcndef">
2041 Return the offset, from the beginning of the current line, of
2042 the position.
2043 </div>
2044
2045 <h4 id="XML_GetCurrentByteCount">XML_GetCurrentByteCount</h4>
2046 <pre class="fcndec">
2047 int XMLCALL
2048 XML_GetCurrentByteCount(XML_Parser p);
2049 </pre>
2050 <div class="fcndef">
2051 Return the number of bytes in the current event. Returns
2052 <code>0</code> if the event is inside a reference to an internal
2053 entity and for the end-tag event for empty element tags (the later can
2054 be used to distinguish empty-element tags from empty elements using
2055 separate start and end tags).
2056 </div>
2057
2058 <h4 id="XML_GetInputContext">XML_GetInputContext</h4>
2059 <pre class="fcndec">
2060 const char * XMLCALL
2061 XML_GetInputContext(XML_Parser p,
2062 int *offset,
2063 int *size);
2064 </pre>
2065 <div class="fcndef">
2066
2067 <p>Returns the parser's input buffer, sets the integer pointed at by
2068 <code>offset</code> to the offset within this buffer of the current
2069 parse position, and set the integer pointed at by <code>size</code> to
2070 the size of the returned buffer.</p>
2071
2072 <p>This should only be called from within a handler during an active
2073 parse and the returned buffer should only be referred to from within
2074 the handler that made the call. This input buffer contains the
2075 untranslated bytes of the input.</p>
2076
2077 <p>Only a limited amount of context is kept, so if the event
2078 triggering a call spans over a very large amount of input, the actual
2079 parse position may be before the beginning of the buffer.</p>
2080
2081 <p>If <code>XML_CONTEXT_BYTES</code> is not defined, this will always
2082 return NULL.</p>
2083 </div>
2084
2085 <h3><a name="billion-laughs">Billion Laughs Attack Protection</a></h3>
2086
2087 <p>The functions in this section configure the built-in
2088 protection against various forms of
2089 <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a>.</p>
2090
2091 <h4 id="XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</h4>
2092 <pre class="fcndec">
2093 /* Added in Expat 2.4.0. */
2094 XML_Bool XMLCALL
2095 XML_SetBillionLaughsAttackProtectionMaximumAmplification(XML_Parser p,
2096 float maximumAmplificationFactor);
2097 </pre>
2098 <div class="fcndef">
2099 <p>
2100 Sets the maximum tolerated amplification factor
2101 for protection against
2102 <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a>
2103 (default: <code>100.0</code>)
2104 of parser <code>p</code> to <code>maximumAmplificationFactor</code>, and
2105 returns <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error.
2106 </p>
2107
2108 The amplification factor is calculated as ..
2109 <pre>
2110 amplification := (direct + indirect) / direct
2111 </pre>
2112 .. while parsing, whereas
2113 <code>direct</code> is the number of bytes read from the primary document in parsing and
2114 <code>indirect</code> is the number of bytes added by expanding entities and reading of external DTD files, combined.
2115
2116 <p>For a call to <code>XML_SetBillionLaughsAttackProtectionMaximumAmplification</code> to succeed:</p>
2117 <ul>
2118 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers) and</li>
2119 <li><code>maximumAmplificationFactor</code> must be non-<code>NaN</code> and greater than or equal to <code>1.0</code>.</li>
2120 </ul>
2121
2122 <p>
2123 <strong>Note:</strong>
2124 If you ever need to increase this value for non-attack payload,
2125 please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>.
2126 </p>
2127
2128 <p>
2129 <strong>Note:</strong>
2130 Peak amplifications
2131 of factor 15,000 for the entire payload and
2132 of factor 30,000 in the middle of parsing
2133 have been observed with small benign files in practice.
2134
2135 So if you do reduce the maximum allowed amplification,
2136 please make sure that the activation threshold is still big enough
2137 to not end up with undesired false positives (i.e. benign files being rejected).
2138 </p>
2139 </div>
2140
2141 <h4 id="XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</h4>
2142 <pre class="fcndec">
2143 /* Added in Expat 2.4.0. */
2144 XML_Bool XMLCALL
2145 XML_SetBillionLaughsAttackProtectionActivationThreshold(XML_Parser p,
2146 unsigned long long activationThresholdBytes);
2147 </pre>
2148 <div class="fcndef">
2149 <p>
2150 Sets number of output bytes (including amplification from entity expansion and reading DTD files)
2151 needed to activate protection against
2152 <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a>
2153 (default: <code>8 MiB</code>)
2154 of parser <code>p</code> to <code>activationThresholdBytes</code>, and
2155 returns <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error.
2156 </p>
2157
2158 <p>For a call to <code>XML_SetBillionLaughsAttackProtectionActivationThreshold</code> to succeed:</p>
2159 <ul>
2160 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers).</li>
2161 </ul>
2162
2163 <p>
2164 <strong>Note:</strong>
2165 If you ever need to increase this value for non-attack payload,
2166 please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>.
2167 </p>
2168
2169 <p>
2170 <strong>Note:</strong>
2171 Activation thresholds below 4 MiB are known to break support for
2172 <a href="https://en.wikipedia.org/wiki/Darwin_Information_Typing_Architecture">DITA</a> 1.3 payload
2173 and are hence not recommended.
2174 </p>
2175 </div>
2176
2177 <h3><a name="miscellaneous">Miscellaneous functions</a></h3>
2178
2179 <p>The functions in this section either obtain state information from
2180 the parser or can be used to dynamically set parser options.</p>
2181
2182 <h4 id="XML_SetUserData">XML_SetUserData</h4>
2183 <pre class="fcndec">
2184 void XMLCALL
2185 XML_SetUserData(XML_Parser p,
2186 void *userData);
2187 </pre>
2188 <div class="fcndef">
2189 This sets the user data pointer that gets passed to handlers. It
2190 overwrites any previous value for this pointer. Note that the
2191 application is responsible for freeing the memory associated with
2192 <code>userData</code> when it is finished with the parser. So if you
2193 call this when there's already a pointer there, and you haven't freed
2194 the memory associated with it, then you've probably just leaked
2195 memory.
2196 </div>
2197
2198 <h4 id="XML_GetUserData">XML_GetUserData</h4>
2199 <pre class="fcndec">
2200 void * XMLCALL
2201 XML_GetUserData(XML_Parser p);
2202 </pre>
2203 <div class="fcndef">
2204 This returns the user data pointer that gets passed to handlers.
2205 It is actually implemented as a macro.
2206 </div>
2207
2208 <h4 id="XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</h4>
2209 <pre class="fcndec">
2210 void XMLCALL
2211 XML_UseParserAsHandlerArg(XML_Parser p);
2212 </pre>
2213 <div class="fcndef">
2214 After this is called, handlers receive the parser in their
2215 <code>userData</code> arguments. The user data can still be obtained
2216 using the <code><a href= "#XML_GetUserData"
2217 >XML_GetUserData</a></code> function.
2218 </div>
2219
2220 <h4 id="XML_SetBase">XML_SetBase</h4>
2221 <pre class="fcndec">
2222 enum XML_Status XMLCALL
2223 XML_SetBase(XML_Parser p,
2224 const XML_Char *base);
2225 </pre>
2226 <div class="fcndef">
2227 Set the base to be used for resolving relative URIs in system
2228 identifiers. The return value is <code>XML_STATUS_ERROR</code> if
2229 there's no memory to store base, otherwise it's
2230 <code>XML_STATUS_OK</code>.
2231 </div>
2232
2233 <h4 id="XML_GetBase">XML_GetBase</h4>
2234 <pre class="fcndec">
2235 const XML_Char * XMLCALL
2236 XML_GetBase(XML_Parser p);
2237 </pre>
2238 <div class="fcndef">
2239 Return the base for resolving relative URIs.
2240 </div>
2241
2242 <h4 id="XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</h4>
2243 <pre class="fcndec">
2244 int XMLCALL
2245 XML_GetSpecifiedAttributeCount(XML_Parser p);
2246 </pre>
2247 <div class="fcndef">
2248 When attributes are reported to the start handler in the atts vector,
2249 attributes that were explicitly set in the element occur before any
2250 attributes that receive their value from default information in an
2251 ATTLIST declaration. This function returns the number of attributes
2252 that were explicitly set times two, thus giving the offset in the
2253 <code>atts</code> array passed to the start tag handler of the first
2254 attribute set due to defaults. It supplies information for the last
2255 call to a start handler. If called inside a start handler, then that
2256 means the current call.
2257 </div>
2258
2259 <h4 id="XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</h4>
2260 <pre class="fcndec">
2261 int XMLCALL
2262 XML_GetIdAttributeIndex(XML_Parser p);
2263 </pre>
2264 <div class="fcndef">
2265 Returns the index of the ID attribute passed in the atts array in the
2266 last call to <code><a href= "#XML_StartElementHandler"
2267 >XML_StartElementHandler</a></code>, or -1 if there is no ID
2268 attribute. If called inside a start handler, then that means the
2269 current call.
2270 </div>
2271
2272 <h4 id="XML_GetAttributeInfo">XML_GetAttributeInfo</h4>
2273 <pre class="fcndec">
2274 const XML_AttrInfo * XMLCALL
2275 XML_GetAttributeInfo(XML_Parser parser);
2276 </pre>
2277 <pre class="signature">
2278 typedef struct {
2279 XML_Index nameStart; /* Offset to beginning of the attribute name. */
2280 XML_Index nameEnd; /* Offset after the attribute name's last byte. */
2281 XML_Index valueStart; /* Offset to beginning of the attribute value. */
2282 XML_Index valueEnd; /* Offset after the attribute value's last byte. */
2283 } XML_AttrInfo;
2284 </pre>
2285 <div class="fcndef">
2286 Returns an array of <code>XML_AttrInfo</code> structures for the
2287 attribute/value pairs passed in the last call to the
2288 <code>XML_StartElementHandler</code> that were specified
2289 in the start-tag rather than defaulted. Each attribute/value pair counts
2290 as 1; thus the number of entries in the array is
2291 <code>XML_GetSpecifiedAttributeCount(parser) / 2</code>.
2292 </div>
2293
2294 <h4 id="XML_SetEncoding">XML_SetEncoding</h4>
2295 <pre class="fcndec">
2296 enum XML_Status XMLCALL
2297 XML_SetEncoding(XML_Parser p,
2298 const XML_Char *encoding);
2299 </pre>
2300 <div class="fcndef">
2301 Set the encoding to be used by the parser. It is equivalent to
2302 passing a non-null encoding argument to the parser creation functions.
2303 It must not be called after <code><a href= "#XML_Parse"
2304 >XML_Parse</a></code> or <code><a href= "#XML_ParseBuffer"
2305 >XML_ParseBuffer</a></code> have been called on the given parser.
2306 Returns <code>XML_STATUS_OK</code> on success or
2307 <code>XML_STATUS_ERROR</code> on error.
2308 </div>
2309
2310 <h4 id="XML_SetParamEntityParsing">XML_SetParamEntityParsing</h4>
2311 <pre class="fcndec">
2312 int XMLCALL
2313 XML_SetParamEntityParsing(XML_Parser p,
2314 enum XML_ParamEntityParsing code);
2315 </pre>
2316 <div class="fcndef">
2317 This enables parsing of parameter entities, including the external
2318 parameter entity that is the external DTD subset, according to
2319 <code>code</code>.
2320 The choices for <code>code</code> are:
2321 <ul>
2322 <li><code>XML_PARAM_ENTITY_PARSING_NEVER</code></li>
2323 <li><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></li>
2324 <li><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></li>
2325 </ul>
2326 <b>Note:</b> If <code>XML_SetParamEntityParsing</code> is called after
2327 <code>XML_Parse</code> or <code>XML_ParseBuffer</code>, then it has
2328 no effect and will always return 0.
2329 </div>
2330
2331 <h4 id="XML_SetHashSalt">XML_SetHashSalt</h4>
2332 <pre class="fcndec">
2333 int XMLCALL
2334 XML_SetHashSalt(XML_Parser p,
2335 unsigned long hash_salt);
2336 </pre>
2337 <div class="fcndef">
2338 Sets the hash salt to use for internal hash calculations.
2339 Helps in preventing DoS attacks based on predicting hash
2340 function behavior. In order to have an effect this must be called
2341 before parsing has started. Returns 1 if successful, 0 when called
2342 after <code>XML_Parse</code> or <code>XML_ParseBuffer</code>.
2343 <p><b>Note:</b> This call is optional, as the parser will auto-generate
2344 a new random salt value if no value has been set at the start of parsing.</p>
2345 <p><b>Note:</b> One should not call <code>XML_SetHashSalt</code> with a
2346 hash salt value of 0, as this value is used as sentinel value to indicate
2347 that <code>XML_SetHashSalt</code> has <b>not</b> been called. Consequently
2348 such a call will have no effect, even if it returns 1.</p>
2349 </div>
2350
2351 <h4 id="XML_UseForeignDTD">XML_UseForeignDTD</h4>
2352 <pre class="fcndec">
2353 enum XML_Error XMLCALL
2354 XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD);
2355 </pre>
2356 <div class="fcndef">
2357 <p>This function allows an application to provide an external subset
2358 for the document type declaration for documents which do not specify
2359 an external subset of their own. For documents which specify an
2360 external subset in their DOCTYPE declaration, the application-provided
2361 subset will be ignored. If the document does not contain a DOCTYPE
2362 declaration at all and <code>useDTD</code> is true, the
2363 application-provided subset will be parsed, but the
2364 <code>startDoctypeDeclHandler</code> and
2365 <code>endDoctypeDeclHandler</code> functions, if set, will not be
2366 called. The setting of parameter entity parsing, controlled using
2367 <code><a href= "#XML_SetParamEntityParsing"
2368 >XML_SetParamEntityParsing</a></code>, will be honored.</p>
2369
2370 <p>The application-provided external subset is read by calling the
2371 external entity reference handler set via <code><a href=
2372 "#XML_SetExternalEntityRefHandler"
2373 >XML_SetExternalEntityRefHandler</a></code> with both
2374 <code>publicId</code> and <code>systemId</code> set to NULL.</p>
2375
2376 <p>If this function is called after parsing has begun, it returns
2377 <code>XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING</code> and ignores
2378 <code>useDTD</code>. If called when Expat has been compiled without
2379 DTD support, it returns
2380 <code>XML_ERROR_FEATURE_REQUIRES_XML_DTD</code>. Otherwise, it
2381 returns <code>XML_ERROR_NONE</code>.</p>
2382
2383 <p><b>Note:</b> For the purpose of checking WFC: Entity Declared, passing
2384 <code>useDTD == XML_TRUE</code> will make the parser behave as if
2385 the document had a DTD with an external subset. This holds true even if
2386 the external entity reference handler returns without action.</p>
2387 </div>
2388
2389 <h4 id="XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</h4>
2390 <pre class="fcndec">
2391 void XMLCALL
2392 XML_SetReturnNSTriplet(XML_Parser parser,
2393 int do_nst);
2394 </pre>
2395 <div class="fcndef">
2396 <p>
2397 This function only has an effect when using a parser created with
2398 <code><a href= "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>,
2399 i.e. when namespace processing is in effect. The <code>do_nst</code>
2400 sets whether or not prefixes are returned with names qualified with a
2401 namespace prefix. If this function is called with <code>do_nst</code>
2402 non-zero, then afterwards namespace qualified names (that is qualified
2403 with a prefix as opposed to belonging to a default namespace) are
2404 returned as a triplet with the three parts separated by the namespace
2405 separator specified when the parser was created. The order of
2406 returned parts is URI, local name, and prefix.</p> <p>If
2407 <code>do_nst</code> is zero, then namespaces are reported in the
2408 default manner, URI then local_name separated by the namespace
2409 separator.</p>
2410 </div>
2411
2412 <h4 id="XML_DefaultCurrent">XML_DefaultCurrent</h4>
2413 <pre class="fcndec">
2414 void XMLCALL
2415 XML_DefaultCurrent(XML_Parser parser);
2416 </pre>
2417 <div class="fcndef">
2418 This can be called within a handler for a start element, end element,
2419 processing instruction or character data. It causes the corresponding
2420 markup to be passed to the default handler set by <code><a
2421 href="#XML_SetDefaultHandler" >XML_SetDefaultHandler</a></code> or
2422 <code><a href="#XML_SetDefaultHandlerExpand"
2423 >XML_SetDefaultHandlerExpand</a></code>. It does nothing if there is
2424 not a default handler.
2425 </div>
2426
2427 <h4 id="XML_ExpatVersion">XML_ExpatVersion</h4>
2428 <pre class="fcndec">
2429 XML_LChar * XMLCALL
2430 XML_ExpatVersion();
2431 </pre>
2432 <div class="fcndef">
2433 Return the library version as a string (e.g. <code>"expat_1.95.1"</code>).
2434 </div>
2435
2436 <h4 id="XML_ExpatVersionInfo">XML_ExpatVersionInfo</h4>
2437 <pre class="fcndec">
2438 struct XML_Expat_Version XMLCALL
2439 XML_ExpatVersionInfo();
2440 </pre>
2441 <pre class="signature">
2442 typedef struct {
2443 int major;
2444 int minor;
2445 int micro;
2446 } XML_Expat_Version;
2447 </pre>
2448 <div class="fcndef">
2449 Return the library version information as a structure.
2450 Some macros are also defined that support compile-time tests of the
2451 library version:
2452 <ul>
2453 <li><code>XML_MAJOR_VERSION</code></li>
2454 <li><code>XML_MINOR_VERSION</code></li>
2455 <li><code>XML_MICRO_VERSION</code></li>
2456 </ul>
2457 Testing these constants is currently the best way to determine if
2458 particular parts of the Expat API are available.
2459 </div>
2460
2461 <h4 id="XML_GetFeatureList">XML_GetFeatureList</h4>
2462 <pre class="fcndec">
2463 const XML_Feature * XMLCALL
2464 XML_GetFeatureList();
2465 </pre>
2466 <pre class="signature">
2467 enum XML_FeatureEnum {
2468 XML_FEATURE_END = 0,
2469 XML_FEATURE_UNICODE,
2470 XML_FEATURE_UNICODE_WCHAR_T,
2471 XML_FEATURE_DTD,
2472 XML_FEATURE_CONTEXT_BYTES,
2473 XML_FEATURE_MIN_SIZE,
2474 XML_FEATURE_SIZEOF_XML_CHAR,
2475 XML_FEATURE_SIZEOF_XML_LCHAR,
2476 XML_FEATURE_NS,
2477 XML_FEATURE_LARGE_SIZE
2478 };
2479
2480 typedef struct {
2481 enum XML_FeatureEnum feature;
2482 XML_LChar *name;
2483 long int value;
2484 } XML_Feature;
2485 </pre>
2486 <div class="fcndef">
2487 <p>Returns a list of "feature" records, providing details on how
2488 Expat was configured at compile time. Most applications should not
2489 need to worry about this, but this information is otherwise not
2490 available from Expat. This function allows code that does need to
2491 check these features to do so at runtime.</p>
2492
2493 <p>The return value is an array of <code>XML_Feature</code>,
2494 terminated by a record with a <code>feature</code> of
2495 <code>XML_FEATURE_END</code> and <code>name</code> of NULL,
2496 identifying the feature-test macros Expat was compiled with. Since an
2497 application that requires this kind of information needs to determine
2498 the type of character the <code>name</code> points to, records for the
2499 <code>XML_FEATURE_SIZEOF_XML_CHAR</code> and
2500 <code>XML_FEATURE_SIZEOF_XML_LCHAR</code> will be located at the
2501 beginning of the list, followed by <code>XML_FEATURE_UNICODE</code>
2502 and <code>XML_FEATURE_UNICODE_WCHAR_T</code>, if they are present at
2503 all.</p>
2504
2505 <p>Some features have an associated value. If there isn't an
2506 associated value, the <code>value</code> field is set to 0. At this
2507 time, the following features have been defined to have values:</p>
2508
2509 <dl>
2510 <dt><code>XML_FEATURE_SIZEOF_XML_CHAR</code></dt>
2511 <dd>The number of bytes occupied by one <code>XML_Char</code>
2512 character.</dd>
2513 <dt><code>XML_FEATURE_SIZEOF_XML_LCHAR</code></dt>
2514 <dd>The number of bytes occupied by one <code>XML_LChar</code>
2515 character.</dd>
2516 <dt><code>XML_FEATURE_CONTEXT_BYTES</code></dt>
2517 <dd>The maximum number of characters of context which can be
2518 reported by <code><a href= "#XML_GetInputContext"
2519 >XML_GetInputContext</a></code>.</dd>
2520 </dl>
2521 </div>
2522
2523 <h4 id="XML_FreeContentModel">XML_FreeContentModel</h4>
2524 <pre class="fcndec">
2525 void XMLCALL
2526 XML_FreeContentModel(XML_Parser parser, XML_Content *model);
2527 </pre>
2528 <div class="fcndef">
2529 Function to deallocate the <code>model</code> argument passed to the
2530 <code>XML_ElementDeclHandler</code> callback set using <code><a
2531 href="#XML_SetElementDeclHandler" >XML_ElementDeclHandler</a></code>.
2532 This function should not be used for any other purpose.
2533 </div>
2534
2535 <p>The following functions allow external code to share the memory
2536 allocator an <code>XML_Parser</code> has been configured to use. This
2537 is especially useful for third-party libraries that interact with a
2538 parser object created by application code, or heavily layered
2539 applications. This can be essential when using dynamically loaded
2540 libraries which use different C standard libraries (this can happen on
2541 Windows, at least).</p>
2542
2543 <h4 id="XML_MemMalloc">XML_MemMalloc</h4>
2544 <pre class="fcndec">
2545 void * XMLCALL
2546 XML_MemMalloc(XML_Parser parser, size_t size);
2547 </pre>
2548 <div class="fcndef">
2549 Allocate <code>size</code> bytes of memory using the allocator the
2550 <code>parser</code> object has been configured to use. Returns a
2551 pointer to the memory or NULL on failure. Memory allocated in this
2552 way must be freed using <code><a href="#XML_MemFree"
2553 >XML_MemFree</a></code>.
2554 </div>
2555
2556 <h4 id="XML_MemRealloc">XML_MemRealloc</h4>
2557 <pre class="fcndec">
2558 void * XMLCALL
2559 XML_MemRealloc(XML_Parser parser, void *ptr, size_t size);
2560 </pre>
2561 <div class="fcndef">
2562 Allocate <code>size</code> bytes of memory using the allocator the
2563 <code>parser</code> object has been configured to use.
2564 <code>ptr</code> must point to a block of memory allocated by <code><a
2565 href="#XML_MemMalloc" >XML_MemMalloc</a></code> or
2566 <code>XML_MemRealloc</code>, or be NULL. This function tries to
2567 expand the block pointed to by <code>ptr</code> if possible. Returns
2568 a pointer to the memory or NULL on failure. On success, the original
2569 block has either been expanded or freed. On failure, the original
2570 block has not been freed; the caller is responsible for freeing the
2571 original block. Memory allocated in this way must be freed using
2572 <code><a href="#XML_MemFree"
2573 >XML_MemFree</a></code>.
2574 </div>
2575
2576 <h4 id="XML_MemFree">XML_MemFree</h4>
2577 <pre class="fcndec">
2578 void XMLCALL
2579 XML_MemFree(XML_Parser parser, void *ptr);
2580 </pre>
2581 <div class="fcndef">
2582 Free a block of memory pointed to by <code>ptr</code>. The block must
2583 have been allocated by <code><a href="#XML_MemMalloc"
2584 >XML_MemMalloc</a></code> or <code>XML_MemRealloc</code>, or be NULL.
2585 </div>
2586
2587 <hr />
2588
2589 <div class="footer">
2590 Found a bug in the documentation?
2591 <a href="https://github.com/libexpat/libexpat/issues">Please file a bug report.</a>
2592 </div>
2593
2594 </div>
2595 </body>
2596 </html>
2597