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