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