reference.html 101 KB

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