Newer
Older
monitord / xmlParser / xmlParser.html
@root root on 23 Jan 2012 31 KB Migration from SVN revision 455
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><!-- InstanceBegin template="/Templates/standardPage.dwt" codeOutsideHTMLIsLocked="true" -->
  3. <head>
  4.  
  5. <!-- InstanceBeginEditable name="doctitle" -->
  6. <title>Small, simple, cross-platform, free and fast C++ XML Parser</title>
  7. <!-- InstanceEndEditable -->
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  9. <!-- InstanceBeginEditable name="head" -->
  10. <!-- InstanceEndEditable -->
  11. </head>
  12.  
  13. <BODY LEFTMARGIN=15 MARGINWIDTH=15 >
  14.  
  15. <H1>
  16. <div align="center"><!-- InstanceBeginEditable name="titre" -->Small, simple,
  17. cross-platform, free and<em><font face="Arial, Helvetica, sans-serif">&nbsp;fast</font></em>
  18. &nbsp;C++ XML Parser<!-- InstanceEndEditable -->
  19. </div>
  20. </H1>
  21. <!-- InstanceBeginEditable name="content" -->
  22. <p>This project started from my frustration that I could not find any simple,
  23. portable XML Parser to use inside my tools (see <a href="http://www.applied-mathematics.net/CONDORManual/CONDORManual.html">CONDOR</a>
  24. for example). Let's look at the well-known Xerces C++ library: the complete
  25. library is 53 MB! (12.1 MB compressed in a zipfile). I am currently developping
  26. many small tools. I am using XML as standard for all my input /ouput configuration
  27. and data files. The source code of my small tools is usually around 600KB. In
  28. these conditions, don't you think that 53MB to be able to read an XML file is
  29. a little bit &quot;too much&quot;? So I created my own XML parser. My XML parser
  30. &quot;library&quot; is composed of only 2 files: a .cpp file and a .h file.
  31. The total size is 104 KB.<br>
  32. <br>
  33. Here is how it works: The XML parser loads a full XML file in memory, it parses
  34. the file and it generates a tree structure representing the XML file. Of course,
  35. you can also parse XML data that you have already stored yourself into a memory
  36. buffer. Thereafter, you can easily &quot;explore&quot; the tree to get your
  37. data. You can also modify the tree using &quot;add&quot; and &quot;delete&quot;
  38. functions and regenerate a formatted XML string from a subtree. Memory management
  39. is totally transparent through the use of smart pointers (in other words, you
  40. will never have to do any new, delete, malloc or free)(&quot;Smart pointers&quot;
  41. are a primitive version of the garbage collector in Java).<br>
  42. <br>
  43. Here are the characteristics of the XMLparser library:
  44. <ul>
  45. <li>Non-validating XML parser written in standard C++ (DTD's or XSD's informations
  46. are ignored). </li>
  47. <li>Cross-plateform: the library is currently used every day on Solaris, Linux
  48. (32bit and 64bit) and Windows to manipulate &quot;small&quot; <a href="http://www.dmg.org/pmml-v3-0.html" target="_top">PMML
  49. documents</a> (10 MB).<br>
  50. The library has been tested and is working flawlessly using the following
  51. compilers: gcc (under linux, Mac OS X Tiger and under many unix flavours),
  52. Visual Studio 6.0, Visual Studio .NET (under Windows 9x,NT,2000,XP,Vista,CE,mobile),
  53. Intel C/C++ compiler, SUN CC compiler, C++ Borland Compiler. The library is
  54. also used under QNX.</li>
  55. <li>The parser builds a tree structure that you can &quot;explore&quot; easily
  56. (DOM-type parser).</li>
  57. <li>The parser can be used to generate XML strings from subtrees (it's called
  58. rendering). You can also save subtrees directly to files (automatic &quot;Byte
  59. Order Mark&quot;-BOM support).</li>
  60. <li> Modification or &quot;from scratch creation&quot; of large XML tree structures
  61. in memory using funtions like <font face="Courier New, Courier, mono">addChild</font>,
  62. <font face="Courier New, Courier, mono">addAttribute</font>,<font face="Courier New, Courier, mono">updateAttribute</font>,<font face="Courier New, Courier, mono">deleteAttribute</font>,...</li>
  63. <li>It's <strong>SIMPLE</strong>: no need to learn how to use dozens of classes:
  64. there is only one simple class: the 'XMLNode' class (that represents one node
  65. of the XML tree).</li>
  66. <li>Very efficient (Efficiency is required to be able to handle <strong>BIG</strong>
  67. files):
  68. <ul>
  69. <li><font size="-1">The string parser is very efficient: It does only one
  70. pass over the XML string to create the tree. It does the minimal amount
  71. of memory allocations. For example: it does NOT use slow STL::String class
  72. but plain, simple and fast C malloc 's. It also allocates large chunk
  73. of memory instead of many small chunks. Inside Visual C++, the &quot;debug
  74. versions&quot; of the memory allocation functions are very slow: Do not
  75. forget to compile in &quot;release mode&quot; to get maximum speed.</font></li>
  76. <li><font size="-1">The &quot;tree exploration&quot; is very efficient because
  77. all operations on the 'XMLNode' class are handled through references:
  78. there are no memory copy, no memory allocation, never. </font></li>
  79. <li><font size="-1">The XML string rendering is very efficient: It does
  80. one pass to compute the total memory size of the XML string and a second
  81. pass to actually create the string. There is thus only one memory allocation
  82. and no extra memory copy. Other libraries are slower because they are
  83. using the string concatenation operator that requires many memory (re-)allocations
  84. and memory copy.</font></li>
  85. </ul>
  86. </li>
  87. <li>In-memory parsing</li>
  88. <li>Supports XML namespaces</li>
  89. <li>Very small and totally stand-alone (not built on top of something else).
  90. Uses only standard &lt;stdio.h&gt; library (and only for the 'fopen' and the
  91. 'fread' functions to load the XML file).</li>
  92. <li>Easy to integrate into you own projects: it's only 2 files! The .h file
  93. does not contain any implementation code. Compilation is thus very fast.</li>
  94. <li>Robust (I used it every day at work since 2004). <br>
  95. Optionnally, if you define the C++ prepocessor directives STRICT_PARSING and/or
  96. APPROXIMATE_PARSING, the library can be &quot;forgiving&quot; in case of errors
  97. inside the XML. <br>
  98. I have tried to respect the XML-specs given at: <a href="http://www.w3.org/TR/REC-xml/" target="_top">http://www.w3.org/TR/REC-xml/</a>
  99. <li>Fully integrated error handling :
  100. <ul>
  101. <li><font size="-1">The string parser gives you the precise position and
  102. type of the error inside the XML string (if an error is detected).</font></li>
  103. <li><font size="-1">The library allows you to &quot;explore&quot; a part
  104. of the tree that is missing. However data extracted from &quot;missing
  105. subtrees&quot; will be NULL. This way, it's really easy to code &quot;error
  106. handling&quot; procedures.</font></li>
  107. </ul>
  108. <li>Thread-safe (however the global parameters &quot;guessUnicodeChar&quot;
  109. and&quot;strictUTF8Parsing&quot; must be unique because they are shared by
  110. all threads).</li>
  111. <li>Full Supports for all character sets: ANSI / UTF-8 / Unicode 16bit / Unicode
  112. 32bit characters support (Windows, Linux, Linux 64 bits &amp; Solaris version
  113. only)
  114. <ul>
  115. <li><font size="-1">For the unicode version of the library: Automatic conversion
  116. to Unicode before parsing (if the input XML file is standard ansi 8bit
  117. characters).</font></li>
  118. <li><font size="-1"> For the ascii version of the library: Automatic conversion
  119. to ascii before parsing (if the input XML file is unicode 16 or 32bit
  120. wide characters). </font> </li>
  121. </ul>
  122. The library is now able to handle successfuly chinese, cyrilic and other extended
  123. characters thanks to an extended UTF-8 support (see this <a href="http://www.applied-mathematics.net/tools/UTF-8-demo.txt">UTF-8-demo</a>
  124. to show the characters available). If you are still experiencing character
  125. encoding problems, I suggest you to convert your XML files to UTF-8 using
  126. a tool like <a href="http://www.gnu.org/software/libiconv/" target="_top">iconv</a>
  127. (precompiled <a href="http://www.applied-mathematics.net/tools/iconv.zip">
  128. win32 binary</a>).</li>
  129. <li>Transparent memory management through the use of smart pointers.</li>
  130. <li> Limited Support for character entities. The current known character entities
  131. are:<BR>
  132. <div align="center">
  133. <TABLE
  134. style="BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid"
  135. cellSpacing=2 cellPadding=4 width="434" border=0>
  136. <TR bgColor=#cccccc>
  137. <TD width="86" vAlign=top class=tabletext>&amp;lt;</TD>
  138. <TD width="33" vAlign=top class=tabletext>&lt; </TD>
  139. <TD width="281" vAlign=top class=tabletext>less than</TD>
  140. </TR>
  141. <TR bgColor=#ffffcc>
  142. <TD class=tabletext vAlign=top>&amp;gt;</TD>
  143. <TD class=tabletext vAlign=top>&gt; </TD>
  144. <TD class=tabletext vAlign=top>greater than</TD>
  145. </TR>
  146. <TR bgColor=#cccccc>
  147. <TD class=tabletext vAlign=top>&amp;amp; </TD>
  148. <TD class=tabletext vAlign=top>&amp;</TD>
  149. <TD class=tabletext vAlign=top>ampersand </TD>
  150. </TR>
  151. <TR bgColor=#ffffcc>
  152. <TD class=tabletext vAlign=top>&amp;apos;</TD>
  153. <TD class=tabletext vAlign=top>' </TD>
  154. <TD class=tabletext vAlign=top>apostrophe</TD>
  155. </TR>
  156. <TR bgColor=#cccccc>
  157. <TD class=tabletext vAlign=top>&amp;quot;</TD>
  158. <TD class=tabletext vAlign=top>"</TD>
  159. <TD class=tabletext vAlign=top>quotation mark</TD>
  160. </TR>
  161. <TR bgColor=#ffffcc>
  162. <TD vAlign=middle class=tabletext>&amp;#x04B;</TD>
  163. <TD vAlign=middle class=tabletext>K</TD>
  164. <TD vAlign=top class=tabletext>direct access to the ascii code of any
  165. character<br>
  166. (in hexadecimal) </TD>
  167. </TR>
  168. <TR bgColor=#cccccc>
  169. <TD class=tabletext vAlign=middle>&amp;#75;</TD>
  170. <TD class=tabletext vAlign=middle>K</TD>
  171. <TD class=tabletext vAlign=top>direct access to the ascii code of any
  172. character<br>
  173. (in standard decimal)</TD>
  174. </TR>
  175. </TABLE>
  176. </div>
  177. </li>
  178. <li>Support for a wide range of clearTags that are containing unformatted text:<br>
  179. <font size="2" face="Courier New, Courier, mono">&lt;![CDATA[ ... ]]&gt;,
  180. &lt;!-- ... --&gt;, &lt;PRE&gt; ... &lt;/PRE&gt;, &lt;Script&gt; ... &lt;/Script&gt;,
  181. &lt;!DOCTYPE ... &gt; </font><br>
  182. Unformatted texts are not parsed by the library and can contain items that
  183. are usually 'forbidden' in XML (for example: html code)</li>
  184. <li>Support for inclusion of pure binary data (images, sounds,...) into the
  185. XML document using the four provided ultrafast Base64 conversion functions.</li>
  186. <li>The library is under the BSD LICENSE. It means that you can use it freely
  187. in all your applications, even the commercial ones, without any restriction.</li>
  188. <li>Easy to customize: The code is small, commented and written in a plain and
  189. simple way. Thus, if you really need to change something (but I doubt of it),
  190. it's easy.</li>
  191. </ul>
  192. <h1>Download</h1>
  193. If you like this library, you can either <a target="_top" href="http://www.applied-mathematics.net/livre_dor/livre_signer.php">add
  194. a message</a> in the <a target="_top" href="http://www.applied-mathematics.net/livre_dor/livre_lire.php">guestbook</a>
  195. or create a URL-Link towards this page from your website (use this URL: <a href="http://www.applied-mathematics.net/tools/xmlParser.html" target="_top">http://www.applied-mathematics.net/tools/xmlParser.html</a>).
  196. If you want to help other people to produce better softwares using XML technology,
  197. you can increase the visibility of this library by adding a URL-link toward this
  198. page (so that its google-ranking increases ;-) ).<br>
  199. <br>
  200. Download here: <a href="http://www.applied-mathematics.net/download.php?id=43">small,
  201. simple, multi-Plateform XMLParser library with examples (zipfile)</a>. <br>
  202. Inside the zip file, you will find 5 examples:
  203. <ul>
  204. <li><em>ansi</em> unix/solaris project example (makefile based)</li>
  205. <li><em>wide char</em> unix/solaris project example (makefile based)</li>
  206. <li><em>ansi</em> windows project example (for Visual Studio 6 and .NET)</li>
  207. <li><em>wide char</em> windows project example (for Visual Studio 6 and .NET)</li>
  208. <li><em>ansi </em>windows .dll project with a small test project to check the
  209. generated .dll</li>
  210. </ul>
  211. <h1>Log</h1>
  212. Version changes:
  213. <ul>
  214. <li><font size="-1">V1.00: February 20, 2002: initial version.</font></li>
  215. <li><font size="-1">V1.20: July 22, 2006: After 13 minor changes, 2 major changes,
  216. 8 bug fixes and 23 functionality additions(at user's request), I decided to
  217. switch to V2.01.</font></li>
  218. <li><font size="-1">V2.01: July 24, 2006: 1 major change, 2 minor change, 3
  219. additions</font>
  220. <ul>
  221. <li><font size="-1">M</font><font size="-1">ajor Change: no more &quot;stringDup&quot;
  222. required for functions like &quot;addText&quot;, &quot;addAttribute&quot;,...
  223. <br>
  224. The old behavior is still accessible through functions like &quot;addText_WOSD&quot;,
  225. &quot;addAttribute_WOSD&quot;,... (&quot;_WSOD&quot; stands for &quot;WithOut
  226. StringDup&quot;).<br>
  227. This change greatly simplifies the user's code. Unfortunately, old user's
  228. code must be updated to work with the new version. <br>
  229. Fortunately, all the user's code used to READ the content of an XML file
  230. is left unchanged: Only the &quot;creation of XML&quot; and the &quot;update
  231. of XML&quot; user's code require a little updating work.</font></li>
  232. </ul>
  233. </li>
  234. <li><font size="-1">V2.02: July 25, 2006: 1 minor change</font></li>
  235. <li><font size="-1">V2.03: July 28, 2006: 1 minor change </font></li>
  236. <li><font size="-1">V2.04: August 6, 2006: 1 addition</font></li>
  237. <li><font size="-1">V2.05: August 15, 2006: 1 addition</font></li>
  238. <li><font size="-1">V2.06: August 16, 2006: 2 additions</font></li>
  239. <li><font size="-1">V2.07: August 22, 2006: 1 addition</font></li>
  240. <li><font size="-1">V2.08: August 22, 2006: 1 bug fix</font> </li>
  241. <li><font size="-1">V2.09: August 31, 2006: 1 bug fix</font> </li>
  242. <li><font size="-1"> V2.10: September 21, 2006: 1 bug fix</font> </li>
  243. <li> <font size="-1">V2.11: October 24, 2006: 3 additions, 1 bug fix. </font>
  244. <ul>
  245. <li><font size="-1"> added the function getParentNode(). Thanks to Jakub
  246. Siudzinski for notifying me a good way to do it easily.</font> </li>
  247. </ul>
  248. </li>
  249. <li><font size="-1">V2.12: October 25, 2006: 2 addition</font>s </li>
  250. <li><font size="-1">V2.13: October 31, 2006: 1 minor change, 1 bug fix</font></li>
  251. <li> <font size="-1">V2.14: November 13, 2006: 1 minor change, 1 bug fix</font></li>
  252. <li><font size="-1"> V2.15: December 22, 2006: 2 additions</font></li>
  253. <li><font size="-1">V2.16: December 27, 2006: 1 minor change</font> </li>
  254. <li><font size="-1"> V2.17: January 9, 2007: 1 addition, 1 minor change</font>
  255. </li>
  256. <li><font size="-1">V2.18: January 15, 2007: 1 bug fix</font></li>
  257. <li><font size="-1">V2.19: January 30, 2007: 1 bug fix, 3 additions</font></li>
  258. <li><font size="-1">V2.20: February 17, 2007: 1 addition</font>
  259. <ul>
  260. <li><font size="-1"> added a Visual Studio projet file to build a DLL version
  261. of the library.<br>
  262. Under Windows, when I have to debug a software that is using the XMLParser
  263. Library, it's usually a nightmare because the library is sooOOOoooo slow
  264. in debug mode. To solve this problem, during all the debugging session,
  265. I use a very fast DLL version of the XMLParser Library (the DLL is compiled
  266. in release mode). Using the DLL version of the XMLParser Library allows
  267. me to have lightening XML parsing speed, even in debug mode! Other than
  268. that, the DLL version is useless: In the release version of my tool, I
  269. always use the normal, &quot;.cpp&quot;-based, XMLParser Library.</font></li>
  270. </ul>
  271. </li>
  272. <li><font size="-1">V2.21: Mars 1, 2007: 1 minor change, 1 bug fix</font> </li>
  273. <li> <font size="-1">V2.22: Mars 6, 2007: 1 bug fix</font> </li>
  274. <li><font size="-1">V2.23: Mars 13, 2007: 1 bug fix</font>
  275. <ul>
  276. <li><font size="-1"> FIX: the library is now thread-safe.</font></li>
  277. </ul>
  278. </li>
  279. </ul>
  280. <h1>A small tutorial</h1>
  281. Let's assume that you want to parse the XML file &quot;<font size="2" face="Courier New, Courier, mono">PMMLModel.xml</font>&quot;
  282. that contains: </p>
  283. <pre><font color="#0033FF">&lt;?xml version=&quot;<strong>1.0</strong>&quot; encoding=&quot;<strong>ISO-8859-1</strong>&quot;?&gt;</font><br><font color="#7F0000"><font color="#0033FF">&lt;</font>PMML version<font color="#0033FF">=&quot;</font><strong><font color="#000000"><font color="#000000">3.0</font></font></strong><font color="#0033FF">&quot;</font><br> xmlns<font color="#0033FF">=&quot;</font><strong><font color="#000000">http://www.dmg.org/PMML-3-0</font></strong><font color="#0033FF">&quot;</font><br> xmlns:xsi<font color="#0033FF">=&quot;</font><strong><font color="#000000">http://www.w3.org/2001/XMLSchema_instance</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>Header copyright<font color="#0033FF">=&quot;</font><strong><font color="#000000">Frank Vanden Berghen</font></strong><font color="#0033FF">&quot;&gt;</font>
  284. <font color="#000000"><strong>Hello World!</strong></font><br> <font color="#0033FF">&lt;</font>Application name<font color="#0033FF">=&quot;</font><strong><font color="#000000">&amp;lt;Condor&gt;</font></strong><font color="#0033FF">&quot;</font> version<font color="#0033FF">=&quot;</font><strong><font color="#000000">1.99beta</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;/</font>Header<font color="#0033FF">&gt;</font>
  285. <font color="#0033FF">&lt;</font>Extension name<font color="#0033FF">=&quot;</font><strong><font color="#000000">keys</font></strong><font color="#0033FF">&quot;</font><font color="#0033FF">&gt;</font> <font color="#0033FF">&lt;</font>Key name<font color="#0033FF">=&quot;</font><strong><font color="#000000">urn</font></strong><font color="#0033FF">&quot;</font><font color="#0033FF">&gt; </font><font color="#0033FF">&lt;/</font>Key<font color="#0033FF">&gt;</font> <font color="#0033FF">&lt;/</font>Extension<font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>DataDictionary<font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>DataField name<font color="#0033FF">=&quot;</font><strong><font color="#000000">persfam</font></strong><font color="#0033FF">&quot;</font> optype<font color="#0033FF">=&quot;</font><strong><font color="#000000">continuous</font></strong><font color="#0033FF">&quot;</font> dataType<font color="#0033FF">=&quot;</font><strong><font color="#000000">double</font></strong><font color="#0033FF">&quot;</font><font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>Value value<font color="#0033FF">=&quot;</font><strong><font color="#000000">9.900000e+001</font></strong><font color="#0033FF">&quot;</font> property<font color="#0033FF">=&quot;</font><strong><font color="#000000">missing</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;/</font>DataField<font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>DataField name<font color="#0033FF">=&quot;</font><strong><font color="#000000">prov</font></strong><font color="#0033FF">&quot;</font> optype<font color="#0033FF">=&quot;</font><font color="#7F0000"><strong><font color="#000000">continuous</font></strong><font color="#0033FF"></font></font><font color="#0033FF">&quot;</font> dataType<font color="#0033FF">=&quot;</font><strong><font color="#000000">double</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;</font>DataField name<font color="#0033FF">=&quot;</font><strong><font color="#000000">urb</font></strong><font color="#0033FF">&quot;</font> optype<font color="#0033FF">=&quot;</font><font color="#7F0000"><strong><font color="#000000">continuous</font></strong><font color="#0033FF"></font></font><font color="#0033FF">&quot;</font> dataType<font color="#0033FF">=&quot;</font><strong><font color="#000000">double</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;</font>DataField name<font color="#0033FF">=&quot;</font><strong><font color="#000000">ses</font></strong><font color="#0033FF">&quot;</font> optype<font color="#0033FF">=&quot;</font><font color="#7F0000"><strong><font color="#000000">continuous</font></strong><font color="#0033FF"></font></font><font color="#0033FF">&quot;</font> dataType<font color="#0033FF">=&quot;</font><strong><font color="#000000">double</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;/</font>DataDictionary<font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>RegressionModel functionName<font color="#0033FF">=&quot;</font><strong><font color="#000000">regression</font></strong><font color="#0033FF">&quot;</font> modelType<font color="#0033FF">=&quot;</font><strong><font color="#000000">linearRegression</font></strong><font color="#0033FF">&quot;</font><font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>RegressionTable intercept<font color="#0033FF">=&quot;</font><strong><font color="#000000">0.00796037</font></strong><font color="#0033FF">&quot;</font><font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;</font>NumericPredictor name<font color="#0033FF">=&quot;</font><strong><font color="#000000">persfam</font></strong><font color="#0033FF">&quot;</font> coefficient<font color="#0033FF">=&quot;</font><strong><font color="#000000">-0.00275951</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;</font>NumericPredictor name<font color="#0033FF">=&quot;</font><strong><font color="#000000">prov</font></strong><font color="#0033FF">&quot;</font> coefficient<font color="#0033FF">=&quot;</font><strong><font color="#000000">0.000319433</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;</font>NumericPredictor name<font color="#0033FF">=&quot;</font><strong><font color="#000000">ses</font></strong><font color="#0033FF">&quot;</font> coefficient<font color="#0033FF">=&quot;</font><strong><font color="#000000">-0.000454307</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font>
  286. <font color="#0033FF">&lt;</font>NONNumericPredictor name<font color="#0033FF">=&quot;</font><strong><font color="#000000">testXmlExample</font></strong><font color="#0033FF">&quot;</font> <font color="#0033FF">/&gt;</font><br> <font color="#0033FF">&lt;/</font>RegressionTable<font color="#0033FF">&gt;</font><br> <font color="#0033FF">&lt;/</font>RegressionModel<font color="#0033FF">&gt;</font><br><font color="#0033FF">&lt;/</font>PMML<font color="#0033FF">&gt;</font></font></pre>
  287. <p>Let's analyse line by line the following small example program:
  288. <pre><font color="#0000FF">#include</font> &lt;stdio.h&gt; <font color="#008000">// to get &quot;printf&quot; function</font>
  289. <font color="#0033FF">#include</font> &lt;stdlib.h&gt; <font color="#008000">// to get &quot;free&quot; function</font>
  290. <font color="#0033FF">#include</font> &quot;<font color="#990000">xmlParser.h</font>&quot;
  291.  
  292. <font color="#0033FF">int</font> main(<font color="#0033FF">int</font> argc, <font color="#0033FF">char</font> **argv)
  293. {
  294. <font color="#008000">// this open and parse the XML file:</font><br> XMLNode xMainNode=XMLNode::openFileHelper(&quot;<font color="#990000">PMMLModel.xml</font>&quot;,&quot;<font color="#990000">PMML</font>&quot;);<br>
  295. <font color="#008000">// this prints &quot;&lt;Condor&gt;&quot;:</font>
  296. XMLNode xNode=xMainNode.getChildNode(&quot;<font color="#990000">Header</font>&quot;);
  297. printf(&quot;<font color="#990000">Application Name is: '%s'\n</font>&quot;, xNode.getChildNode(&quot;<font color="#990000">Application</font>&quot;).getAttribute(&quot;<font color="#990000">name</font>&quot;));<br> <font color="#008000">
  298. // this prints &quot;Hello world!&quot;:</font>
  299. printf(&quot;<font color="#990000">Text inside Header tag is :'%s'\n</font>&quot;, xNode.getText());<br>
  300. <font color="#008000">// this gets the number of &quot;NumericPredictor&quot; tags:</font><br> xNode=xMainNode.getChildNode(&quot;<font color="#990000">RegressionModel</font>&quot;).getChildNode(&quot;<font color="#990000">RegressionTable</font>&quot;);
  301. <font color="#0033FF">int</font> n=xNode.nChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;);
  302.  
  303. <font color="#008000">// this prints the &quot;coefficient&quot; value for all the &quot;NumericPredictor&quot; tags:</font><br> for (<font color="#0033FF">int</font> i=0; i&lt;n; i++)
  304. printf(&quot;<font color="#990000">coeff %i=%f\n</font>&quot;,i+1,atof(xNode.getChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;,i).getAttribute(&quot;<font color="#990000">coefficient</font>&quot;)));
  305.  
  306. <font color="#008000">// this prints a formatted ouput based on the content of the first &quot;Extension&quot; tag of the XML file:</font><br> <font color="#0033FF">char</font> *t=xMainNode.getChildNode(&quot;<font color="#990000">Extension</font>&quot;).createXMLString(<font color="#0033FF">true</font>);<br> printf(&quot;<font color="#990000">%s\n</font>&quot;,t);<br> free(t);
  307. <font color="#0033FF">return</font> 0;<br>}</pre>
  308. <p>To manipulate the data contained inside the XML file, the first operation is
  309. to get an instance of the class XMLNode that is representing the XML file in
  310. memory. You can use:
  311. <pre>XMLNode xMainNode=XMLNode::openFileHelper(&quot;<font color="#990000">PMMLModel.xml</font>&quot;,&quot;<font color="#990000">PMML</font>&quot;);</pre>
  312. or, if you use the UNICODE windows version of the library:
  313. <pre>XMLNode xMainNode=XMLNode::openFileHelper(&quot;<font color="#990000">PMMLModel.xml</font>&quot;,_T(&quot;<font color="#990000">PMML</font>&quot;));</pre>
  314. or, if the XML document is already in a memory buffer pointed by variable &quot;<font size="2" face="Courier New, Courier, mono">char
  315. *xmlDoc</font>&quot; :
  316. <pre>XMLNode xMainNode=XMLNode::parseString(xmlDoc,&quot;<font color="#990000">PMML</font>&quot;);</pre>
  317. This will create an object called <font size="2" face="Courier New, Courier, mono">xMainNode</font>
  318. that represents the first tag named <font size="2" face="Courier New, Courier, mono">PMML</font>
  319. found inside the XML document. This object is the top of tree structure representing
  320. the XML file in memory. The following command creates a new object called <font size="2" face="Courier New, Courier, mono">xNode</font>
  321. that represents the &quot;<font size="2" face="Courier New, Courier, mono">Header</font>&quot;
  322. tag inside the &quot;<font size="2" face="Courier New, Courier, mono">PMML</font>&quot;
  323. tag. </p>
  324. <pre>XMLNode xNode=xMainNode.getChildNode(&quot;<font color="#990000">Header</font>&quot;);</pre>
  325. The following command prints on the screen &quot;<font size="2" face="Courier New, Courier, mono">&lt;Condor&gt;</font>&quot;
  326. (note that the &quot;<font size="2" face="Courier New, Courier, mono">&amp;lt;</font>&quot;
  327. character entity has been replaced by &quot;&lt;&quot;):
  328. <pre>printf(&quot;<font color="#990000">Application Name is: '%S'\n</font>&quot;, xNode.getChildNode(&quot;<font color="#990000">Application</font>&quot;).getAttribute(&quot;<font color="#990000">name</font>&quot;));</pre>
  329. The following command prints on the screen &quot;<font size="2" face="Courier New, Courier, mono">Hello
  330. World!</font>&quot;:
  331. <pre>printf(&quot;<font color="#990000">Text inside Header tag is :'%s'\n</font>&quot;, xNode.getText());</pre>
  332. Let's assume you want to &quot;go to&quot; the tag named &quot;<font size="2" face="Courier New, Courier, mono">RegressionTable</font>&quot;: </p>
  333. <pre>xNode=xMainNode.getChildNode(&quot;<font color="#990000">RegressionModel</font>&quot;).getChildNode(&quot;<font color="#990000">RegressionTable</font>&quot;);</pre>
  334. <p>Note that the previous value of the object named <font size="2" face="Courier New, Courier, mono">xNode</font>
  335. has been &quot;garbage collected&quot; so that no memory leak occurs. If you
  336. want to know how many tags named &quot;<font size="2" face="Courier New, Courier, mono">NumericPredictor</font>&quot;
  337. are contained inside the tag named &quot;<font size="2" face="Courier New, Courier, mono">RegressionTable</font>&quot;:</p>
  338. <pre><font color="#0033FF">int</font> n=xNode.nChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;);</pre>
  339. <p>The variable <font size="2" face="Courier New, Courier, mono">n</font> now
  340. contains the value 3. If you want to print the value of the <font size="2" face="Courier New, Courier, mono">coefficient</font>
  341. attribute for all the <font size="2" face="Courier New, Courier, mono">NumericPredictor</font>
  342. tags:
  343. <pre>for (<font color="#0033FF">int</font> i=0; i&lt;n; i++)
  344. printf(&quot;<font color="#990000">coeff %i=%f\n</font>&quot;,i+1,atof(xNode.getChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;,i).getAttribute(&quot;<font color="#990000">coefficient</font>&quot;)));</pre>
  345. Or equivalently, but faster at runtime:<br>
  346. <pre>
  347. <font color="#0033FF">int</font> iterator=0;
  348. for (<font color="#0033FF">int</font> i=0; i&lt;n; i++)
  349. printf(&quot;<font color="#990000">coeff %i=%f\n</font>&quot;,i+1,atof(xNode.getChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;,&amp;iterator).getAttribute(&quot;<font color="#990000">coefficient</font>&quot;)));</pre>
  350. <p>If you want to generate and print on the screen the following XML formatted
  351. text: </p>
  352. <pre><font color="#7F0000"><font color="#0033FF">&lt;</font>Extension name<font color="#0033FF">=&quot;</font><strong><font color="#000000">keys</font></strong><font color="#0033FF">&quot;</font><font color="#0033FF">&gt;</font>
  353. <font color="#0033FF">&lt;</font>Key name<font color="#0033FF">=&quot;</font><strong><font color="#000000">urn</font></strong><font color="#0033FF">&quot; /&gt;
  354. &lt;/</font>Extension<font color="#0033FF">&gt;</font></font>
  355. </pre>
  356. <p>You can use:
  357. <pre><font color="#0033FF">char</font> *t=xMainNode.getChildNode(&quot;<font color="#990000">Extension</font>&quot;).createXMLString(<font color="#0033FF">true</font>);<br>printf(&quot;<font color="#990000">%s\n</font>&quot;,t);<br>free(t);</pre>
  358. <p>Note that you must free the memory yourself (using the <font size="2" face="Courier New, Courier, mono">&quot;free(t);</font>&quot;
  359. function) : only the XMLNode objects and their contents are &quot;garbage collected&quot;.
  360. The parameter <font size="2" face="Courier New, Courier, mono">true</font> to
  361. the function <font size="2" face="Courier New, Courier, mono">createXMLString</font>
  362. means that we want formatted output. <br>
  363. <br>
  364. The XML Parser library contains many more other small usefull methods that are
  365. not described here (The zip file contains some additional examples to explain
  366. other functionalities). These methods allows you to:
  367. <ul>
  368. <li>navigate easily inside the structure of the XML document</li>
  369. <li>create, update &amp; save your own XML structure of <font face="Courier New, Courier, mono">XMLNode</font>'s.</li>
  370. </ul>
  371. That's all folks! With this basic knowledge, you should be able to retreive easily
  372. any data from any XML file!</p> <!-- InstanceEndEditable --> <br>
  373. </BODY>
  374. <!-- InstanceEnd -->