reference.html 101 KB

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