📄 xmlparser.html
字号:
<li><font size="-1">V2.20: February 17, 2007: 1 addition</font>
<ul>
<li><font size="-1"> added a Visual Studio projet file to build a DLL version
of the library.<br>
Under Windows, when I have to debug a software that is using the XMLParser
Library, it's usually a nightmare because the library is sooOOOoooo slow
in debug mode. To solve this problem, during all the debugging session,
I use a very fast DLL version of the XMLParser Library (the DLL is compiled
in release mode). Using the DLL version of the XMLParser Library allows
me to have lightening XML parsing speed, even in debug mode! Other than
that, the DLL version is useless: In the release version of my tool, I
always use the normal, ".cpp"-based, XMLParser Library.</font></li>
</ul>
</li>
<li><font size="-1">V2.21: Mars 1, 2007: 1 minor change, 1 bug fix</font> </li>
<li> <font size="-1">V2.22: Mars 6, 2007: 1 bug fix</font> </li>
<li><font size="-1">V2.23: Mars 13, 2007: 1 bug fix</font> </li>
<li><font size="-1"> V2.24: April 24, 2007: 1 bug fix, 1 addition</font> </li>
<li><font size="-1">V2.25: May 18, 2007: 1 bug fix</font></li>
<li><font size="-1"> V2.26: May 22, 2007: 1 bug fix</font></li>
<li><font size="-1"> V2.27: May 28, 2007: 2 additions, 1 minor change, 2 bug
fixes</font></li>
<li><font size="-1"> V2.28: June 27, 2007: 2 additions, 2 minor changes</font>
</li>
<li><font size="-1">v2.29: July 3,2007: 1 bug fix </font> </li>
<li><font size="-1"> v2.30: July 31,2007: 2 bug fixes, 1 addition</font></li>
<li><font size="-1"> v2.31: August 29,2007: 1 fix</font>
<ul>
<li><font size="-1"> FIX: when we parse an empty file, we now close properly
the FILE* handle.</font></li>
</ul>
</li>
</ul>
<h1>A small tutorial</h1>
Let's assume that you want to parse the XML file "<font size="2" face="Courier New, Courier, mono">PMMLModel.xml</font>"
that contains: </p>
<pre><font color="#0033FF"><?xml version="<strong>1.0</strong>" encoding="<strong>ISO-8859-1</strong>"?></font><br><font color="#7F0000"><font color="#0033FF"><</font>PMML version<font color="#0033FF">="</font><strong><font color="#000000"><font color="#000000">3.0</font></font></strong><font color="#0033FF">"</font><br> xmlns<font color="#0033FF">="</font><strong><font color="#000000">http://www.dmg.org/PMML-3-0</font></strong><font color="#0033FF">"</font><br> xmlns:xsi<font color="#0033FF">="</font><strong><font color="#000000">http://www.w3.org/2001/XMLSchema_instance</font></strong><font color="#0033FF">"</font> <font color="#0033FF">></font><br> <font color="#0033FF"><</font>Header copyright<font color="#0033FF">="</font><strong><font color="#000000">Frank Vanden Berghen</font></strong><font color="#0033FF">"></font>
<font color="#000000"><strong>Hello World!</strong></font><br> <font color="#0033FF"><</font>Application name<font color="#0033FF">="</font><strong><font color="#000000">&lt;Condor></font></strong><font color="#0033FF">"</font> version<font color="#0033FF">="</font><strong><font color="#000000">1.99beta</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"></</font>Header<font color="#0033FF">></font>
<font color="#0033FF"><</font>Extension name<font color="#0033FF">="</font><strong><font color="#000000">keys</font></strong><font color="#0033FF">"</font><font color="#0033FF">></font> <font color="#0033FF"><</font>Key name<font color="#0033FF">="</font><strong><font color="#000000">urn</font></strong><font color="#0033FF">"</font><font color="#0033FF">> </font><font color="#0033FF"></</font>Key<font color="#0033FF">></font> <font color="#0033FF"></</font>Extension<font color="#0033FF">></font><br> <font color="#0033FF"><</font>DataDictionary<font color="#0033FF">></font><br> <font color="#0033FF"><</font>DataField name<font color="#0033FF">="</font><strong><font color="#000000">persfam</font></strong><font color="#0033FF">"</font> optype<font color="#0033FF">="</font><strong><font color="#000000">continuous</font></strong><font color="#0033FF">"</font> dataType<font color="#0033FF">="</font><strong><font color="#000000">double</font></strong><font color="#0033FF">"</font><font color="#0033FF">></font><br> <font color="#0033FF"><</font>Value value<font color="#0033FF">="</font><strong><font color="#000000">9.900000e+001</font></strong><font color="#0033FF">"</font> property<font color="#0033FF">="</font><strong><font color="#000000">missing</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"></</font>DataField<font color="#0033FF">></font><br> <font color="#0033FF"><</font>DataField name<font color="#0033FF">="</font><strong><font color="#000000">prov</font></strong><font color="#0033FF">"</font> optype<font color="#0033FF">="</font><font color="#7F0000"><strong><font color="#000000">continuous</font></strong><font color="#0033FF"></font></font><font color="#0033FF">"</font> dataType<font color="#0033FF">="</font><strong><font color="#000000">double</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"><</font>DataField name<font color="#0033FF">="</font><strong><font color="#000000">urb</font></strong><font color="#0033FF">"</font> optype<font color="#0033FF">="</font><font color="#7F0000"><strong><font color="#000000">continuous</font></strong><font color="#0033FF"></font></font><font color="#0033FF">"</font> dataType<font color="#0033FF">="</font><strong><font color="#000000">double</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"><</font>DataField name<font color="#0033FF">="</font><strong><font color="#000000">ses</font></strong><font color="#0033FF">"</font> optype<font color="#0033FF">="</font><font color="#7F0000"><strong><font color="#000000">continuous</font></strong><font color="#0033FF"></font></font><font color="#0033FF">"</font> dataType<font color="#0033FF">="</font><strong><font color="#000000">double</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"></</font>DataDictionary<font color="#0033FF">></font><br> <font color="#0033FF"><</font>RegressionModel functionName<font color="#0033FF">="</font><strong><font color="#000000">regression</font></strong><font color="#0033FF">"</font> modelType<font color="#0033FF">="</font><strong><font color="#000000">linearRegression</font></strong><font color="#0033FF">"</font><font color="#0033FF">></font><br> <font color="#0033FF"><</font>RegressionTable intercept<font color="#0033FF">="</font><strong><font color="#000000">0.00796037</font></strong><font color="#0033FF">"</font><font color="#0033FF">></font><br> <font color="#0033FF"><</font>NumericPredictor name<font color="#0033FF">="</font><strong><font color="#000000">persfam</font></strong><font color="#0033FF">"</font> coefficient<font color="#0033FF">="</font><strong><font color="#000000">-0.00275951</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"><</font>NumericPredictor name<font color="#0033FF">="</font><strong><font color="#000000">prov</font></strong><font color="#0033FF">"</font> coefficient<font color="#0033FF">="</font><strong><font color="#000000">0.000319433</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"><</font>NumericPredictor name<font color="#0033FF">="</font><strong><font color="#000000">ses</font></strong><font color="#0033FF">"</font> coefficient<font color="#0033FF">="</font><strong><font color="#000000">-0.000454307</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font>
<font color="#0033FF"><</font>NONNumericPredictor name<font color="#0033FF">="</font><strong><font color="#000000">testXmlExample</font></strong><font color="#0033FF">"</font> <font color="#0033FF">/></font><br> <font color="#0033FF"></</font>RegressionTable<font color="#0033FF">></font><br> <font color="#0033FF"></</font>RegressionModel<font color="#0033FF">></font><br><font color="#0033FF"></</font>PMML<font color="#0033FF">></font></font></pre>
<p>Let's analyse line by line the following small example program:
<pre><font color="#0000FF">#include</font> <stdio.h> <font color="#008000">// to get "printf" function</font>
<font color="#0033FF">#include</font> <stdlib.h> <font color="#008000">// to get "free" function</font>
<font color="#0033FF">#include</font> "<font color="#990000">xmlParser.h</font>"
<font color="#0033FF">int</font> main(<font color="#0033FF">int</font> argc, <font color="#0033FF">char</font> **argv)
{
<font color="#008000">// this open and parse the XML file:</font><br> XMLNode xMainNode=XMLNode::openFileHelper("<font color="#990000">PMMLModel.xml</font>","<font color="#990000">PMML</font>");<br>
<font color="#008000">// this prints "<Condor>":</font>
XMLNode xNode=xMainNode.getChildNode("<font color="#990000">Header</font>");
printf("<font color="#990000">Application Name is: '%s'\n</font>", xNode.getChildNode("<font color="#990000">Application</font>").getAttribute("<font color="#990000">name</font>"));<br> <font color="#008000">
// this prints "Hello world!":</font>
printf("<font color="#990000">Text inside Header tag is :'%s'\n</font>", xNode.getText());<br>
<font color="#008000">// this gets the number of "NumericPredictor" tags:</font><br> xNode=xMainNode.getChildNode("<font color="#990000">RegressionModel</font>").getChildNode("<font color="#990000">RegressionTable</font>");
<font color="#0033FF">int</font> n=xNode.nChildNode("<font color="#990000">NumericPredictor</font>");
<font color="#008000">// this prints the "coefficient" value for all the "NumericPredictor" tags:</font><br> for (<font color="#0033FF">int</font> i=0; i<n; i++)
printf("<font color="#990000">coeff %i=%f\n</font>",i+1,atof(xNode.getChildNode("<font color="#990000">NumericPredictor</font>",i).getAttribute("<font color="#990000">coefficient</font>")));
<font color="#008000">// this prints a formatted ouput based on the content of the first "Extension" tag of the XML file:</font><br> <font color="#0033FF">char</font> *t=xMainNode.getChildNode("<font color="#990000">Extension</font>").createXMLString(<font color="#0033FF">true</font>);<br> printf("<font color="#990000">%s\n</font>",t);<br> free(t);
<font color="#0033FF">return</font> 0;<br>}</pre>
<p>To manipulate the data contained inside the XML file, the first operation is
to get an instance of the class XMLNode that is representing the XML file in
memory. You can use:
<pre>XMLNode xMainNode=XMLNode::openFileHelper("<font color="#990000">PMMLModel.xml</font>","<font color="#990000">PMML</font>");</pre>
or, if you use the UNICODE windows version of the library:
<pre>XMLNode xMainNode=XMLNode::openFileHelper("<font color="#990000">PMMLModel.xml</font>",_T("<font color="#990000">PMML</font>"));</pre>
or, if the XML document is already in a memory buffer pointed by variable "<font size="2" face="Courier New, Courier, mono">char
*xmlDoc</font>" :
<pre>XMLNode xMainNode=XMLNode::parseString(xmlDoc,"<font color="#990000">PMML</font>");</pre>
This will create an object called <font size="2" face="Courier New, Courier, mono">xMainNode</font>
that represents the first tag named <font size="2" face="Courier New, Courier, mono">PMML</font>
found inside the XML document. This object is the top of tree structure representing
the XML file in memory. The following command creates a new object called <font size="2" face="Courier New, Courier, mono">xNode</font>
that represents the "<font size="2" face="Courier New, Courier, mono">Header</font>"
tag inside the "<font size="2" face="Courier New, Courier, mono">PMML</font>"
tag. </p>
<pre>XMLNode xNode=xMainNode.getChildNode("<font color="#990000">Header</font>");</pre>
The following command prints on the screen "<font size="2" face="Courier New, Courier, mono"><Condor></font>"
(note that the "<font size="2" face="Courier New, Courier, mono">&lt;</font>"
character entity has been replaced by "<"):
<pre>printf("<font color="#990000">Application Name is: '%S'\n</font>", xNode.getChildNode("<font color="#990000">Application</font>").getAttribute("<font color="#990000">name</font>"));</pre>
The following command prints on the screen "<font size="2" face="Courier New, Courier, mono">Hello
World!</font>":
<pre>printf("<font color="#990000">Text inside Header tag is :'%s'\n</font>", xNode.getText());</pre>
Let's assume you want to "go to" the tag named "<font size="2" face="Courier New, Courier, mono">RegressionTable</font>": </p>
<pre>xNode=xMainNode.getChildNode("<font color="#990000">RegressionModel</font>").getChildNode("<font color="#990000">RegressionTable</font>");</pre>
<p>Note that the previous value of the object named <font size="2" face="Courier New, Courier, mono">xNode</font>
has been "garbage collected" so that no memory leak occurs. If you
want to know how many tags named "<font size="2" face="Courier New, Courier, mono">NumericPredictor</font>"
are contained inside the tag named "<font size="2" face="Courier New, Courier, mono">RegressionTable</font>":</p>
<pre><font color="#0033FF">int</font> n=xNode.nChildNode("<font color="#990000">NumericPredictor</font>");</pre>
<p>The variable <font size="2" face="Courier New, Courier, mono">n</font> now
contains the value 3. If you want to print the value of the <font size="2" face="Courier New, Courier, mono">coefficient</font>
attribute for all the <font size="2" face="Courier New, Courier, mono">NumericPredictor</font>
tags:
<pre>for (<font color="#0033FF">int</font> i=0; i<n; i++)
printf("<font color="#990000">coeff %i=%f\n</font>",i+1,atof(xNode.getChildNode("<font color="#990000">NumericPredictor</font>",i).getAttribute("<font color="#990000">coefficient</font>")));</pre>
Or equivalently, but faster at runtime:<br>
<pre>
<font color="#0033FF">int</font> iterator=0;
for (<font color="#0033FF">int</font> i=0; i<n; i++)
printf("<font color="#990000">coeff %i=%f\n</font>",i+1,atof(xNode.getChildNode("<font color="#990000">NumericPredictor</font>",&iterator).getAttribute("<font color="#990000">coefficient</font>")));</pre>
<p>If you want to generate and print on the screen the following XML formatted
text: </p>
<pre><font color="#7F0000"><font color="#0033FF"><</font>Extension name<font color="#0033FF">="</font><strong><font color="#000000">keys</font></strong><font color="#0033FF">"</font><font color="#0033FF">></font>
<font color="#0033FF"><</font>Key name<font color="#0033FF">="</font><strong><font color="#000000">urn</font></strong><font color="#0033FF">" />
</</font>Extension<font color="#0033FF">></font></font>
</pre>
<p>You can use:
<pre><font color="#0033FF">char</font> *t=xMainNode.getChildNode("<font color="#990000">Extension</font>").createXMLString(<font color="#0033FF">true</font>);<br>printf("<font color="#990000">%s\n</font>",t);<br>free(t);</pre>
<p>Note that you must free the memory yourself (using the <font size="2" face="Courier New, Courier, mono">"free(t);</font>"
function) : only the XMLNode objects and their contents are "garbage collected".
The parameter <font size="2" face="Courier New, Courier, mono">true</font> to
the function <font size="2" face="Courier New, Courier, mono">createXMLString</font>
means that we want formatted output. <br>
<br>
The XML Parser library contains many more other small usefull methods that are
not described here (The zip file contains some additional examples to explain
other functionalities). These methods allows you to:
<ul>
<li>navigate easily inside the structure of the XML document</li>
<li>create, update & save your own XML structure of <font face="Courier New, Courier, mono">XMLNode</font>'s.</li>
</ul>
That's all folks! With this basic knowledge, you should be able to retreive easily
any data from any XML file!</p> <!-- InstanceEndEditable --> <br>
</BODY>
<!-- InstanceEnd -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -