reference.html 106 KB

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