⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlparser.html

📁 xml文件解析器 解析所有的xml文件 支持unicode
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  <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, &quot;.cpp&quot;-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 &quot;<font size="2" face="Courier New, Courier, mono">PMMLModel.xml</font>&quot; 
that contains: </p> 
<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>
     <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>
  <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>
      <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>
<p>Let's analyse line by line the following small example program: 
<pre><font color="#0000FF">#include</font> &lt;stdio.h&gt;    <font color="#008000">// to get &quot;printf&quot; function</font>
<font color="#0033FF">#include</font> &lt;stdlib.h&gt;   <font color="#008000">// to get &quot;free&quot; function</font>
<font color="#0033FF">#include</font> &quot;<font color="#990000">xmlParser.h</font>&quot;

<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(&quot;<font color="#990000">PMMLModel.xml</font>&quot;,&quot;<font color="#990000">PMML</font>&quot;);<br>
  <font color="#008000">// this prints &quot;&lt;Condor&gt;&quot;:</font>
  XMLNode xNode=xMainNode.getChildNode(&quot;<font color="#990000">Header</font>&quot;);
  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">
  // this prints &quot;Hello world!&quot;:</font>
  printf(&quot;<font color="#990000">Text inside Header tag is :'%s'\n</font>&quot;, xNode.getText());<br>
  <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;);
  <font color="#0033FF">int</font> n=xNode.nChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;);

  <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++)
    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;)));

  <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);
  <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(&quot;<font color="#990000">PMMLModel.xml</font>&quot;,&quot;<font color="#990000">PMML</font>&quot;);</pre>
or, if you use the UNICODE windows version of the library: 
<pre>XMLNode xMainNode=XMLNode::openFileHelper(&quot;<font color="#990000">PMMLModel.xml</font>&quot;,_T(&quot;<font color="#990000">PMML</font>&quot;));</pre>
or, if the XML document is already in a memory buffer pointed by variable &quot;<font size="2" face="Courier New, Courier, mono">char 
*xmlDoc</font>&quot; : 
<pre>XMLNode xMainNode=XMLNode::parseString(xmlDoc,&quot;<font color="#990000">PMML</font>&quot;);</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 &quot;<font size="2" face="Courier New, Courier, mono">Header</font>&quot; 
tag inside the &quot;<font size="2" face="Courier New, Courier, mono">PMML</font>&quot; 
tag. </p> 
<pre>XMLNode xNode=xMainNode.getChildNode(&quot;<font color="#990000">Header</font>&quot;);</pre>
The following command prints on the screen &quot;<font size="2" face="Courier New, Courier, mono">&lt;Condor&gt;</font>&quot; 
(note that the &quot;<font size="2" face="Courier New, Courier, mono">&amp;lt;</font>&quot; 
character entity has been replaced by &quot;&lt;&quot;): 
<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>
The following command prints on the screen &quot;<font size="2" face="Courier New, Courier, mono">Hello 
World!</font>&quot;: 
<pre>printf(&quot;<font color="#990000">Text inside Header tag is :'%s'\n</font>&quot;, xNode.getText());</pre>
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> 
<pre>xNode=xMainNode.getChildNode(&quot;<font color="#990000">RegressionModel</font>&quot;).getChildNode(&quot;<font color="#990000">RegressionTable</font>&quot;);</pre>
<p>Note that the previous value of the object named <font size="2" face="Courier New, Courier, mono">xNode</font> 
  has been &quot;garbage collected&quot; so that no memory leak occurs. If you 
  want to know how many tags named &quot;<font size="2" face="Courier New, Courier, mono">NumericPredictor</font>&quot; 
  are contained inside the tag named &quot;<font size="2" face="Courier New, Courier, mono">RegressionTable</font>&quot;:</p>
<pre><font color="#0033FF">int</font> n=xNode.nChildNode(&quot;<font color="#990000">NumericPredictor</font>&quot;);</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&lt;n; i++)
  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>
Or equivalently, but faster at runtime:<br>
<pre>
<font color="#0033FF">int</font> iterator=0;
for (<font color="#0033FF">int</font> i=0; i&lt;n; i++)
  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>
<p>If you want to generate and print on the screen the following XML formatted 
  text: </p>
<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>
  <font color="#0033FF">&lt;</font>Key name<font color="#0033FF">=&quot;</font><strong><font color="#000000">urn</font></strong><font color="#0033FF">&quot; /&gt;
&lt;/</font>Extension<font color="#0033FF">&gt;</font></font>
</pre>
<p>You can use: 
<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>
<p>Note that you must free the memory yourself (using the <font size="2" face="Courier New, Courier, mono">&quot;free(t);</font>&quot; 
  function) : only the XMLNode objects and their contents are &quot;garbage collected&quot;. 
  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 &amp; 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 + -