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