Home | History | Annotate | Download | only in docs
      1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      2 <html xmlns="http://www.w3.org/1999/xhtml">
      3 <head>
      4 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
      5 <meta http-equiv="X-UA-Compatible" content="IE=9"/>
      6 <meta name="generator" content="Doxygen 1.8.13"/>
      7 <meta name="viewport" content="width=device-width, initial-scale=1"/>
      8 <title>TinyXML-2: Get information out of XML</title>
      9 <link href="tabs.css" rel="stylesheet" type="text/css"/>
     10 <script type="text/javascript" src="jquery.js"></script>
     11 <script type="text/javascript" src="dynsections.js"></script>
     12 <link href="search/search.css" rel="stylesheet" type="text/css"/>
     13 <script type="text/javascript" src="search/searchdata.js"></script>
     14 <script type="text/javascript" src="search/search.js"></script>
     15 <link href="doxygen.css" rel="stylesheet" type="text/css" />
     16 </head>
     17 <body>
     18 <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
     19 <div id="titlearea">
     20 <table cellspacing="0" cellpadding="0">
     21  <tbody>
     22  <tr style="height: 56px;">
     23   <td id="projectalign" style="padding-left: 0.5em;">
     24    <div id="projectname">TinyXML-2
     25    &#160;<span id="projectnumber">7.0.0</span>
     26    </div>
     27   </td>
     28  </tr>
     29  </tbody>
     30 </table>
     31 </div>
     32 <!-- end header part -->
     33 <!-- Generated by Doxygen 1.8.13 -->
     34 <script type="text/javascript">
     35 var searchBox = new SearchBox("searchBox", "search",false,'Search');
     36 </script>
     37 <script type="text/javascript" src="menudata.js"></script>
     38 <script type="text/javascript" src="menu.js"></script>
     39 <script type="text/javascript">
     40 $(function() {
     41   initMenu('',true,false,'search.php','Search');
     42   $(document).ready(function() { init_search(); });
     43 });
     44 </script>
     45 <div id="main-nav"></div>
     46 <!-- window showing the filter options -->
     47 <div id="MSearchSelectWindow"
     48      onmouseover="return searchBox.OnSearchSelectShow()"
     49      onmouseout="return searchBox.OnSearchSelectHide()"
     50      onkeydown="return searchBox.OnSearchSelectKey(event)">
     51 </div>
     52 
     53 <!-- iframe showing the search results (closed by default) -->
     54 <div id="MSearchResultsWindow">
     55 <iframe src="javascript:void(0)" frameborder="0" 
     56         name="MSearchResults" id="MSearchResults">
     57 </iframe>
     58 </div>
     59 
     60 </div><!-- top -->
     61 <div class="header">
     62   <div class="headertitle">
     63 <div class="title">Get information out of XML </div>  </div>
     64 </div><!--header-->
     65 <div class="contents">
     66 <div class="textblock"> In this example, we navigate a simple XML file, and read some interesting text. Note that this example doesn't use error checking; working code should check for null pointers when walking an XML tree, or use XMLHandle.</p>
     67 <p>(The XML is an excerpt from "dream.xml").</p>
     68 <p><div class="fragment"><div class="line"><span class="keywordtype">int</span> example_3()</div><div class="line">{</div><div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* xml =</div><div class="line">        <span class="stringliteral">&quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;&lt;!DOCTYPE PLAY SYSTEM \&quot;play.dtd\&quot;&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;&lt;PLAY&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;&lt;TITLE&gt;A Midsummer Night&#39;s Dream&lt;/TITLE&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;&lt;/PLAY&gt;&quot;</span>;</div></div><!-- fragment --></p>
     69 <p>The structure of the XML file is:</p>
     70 <ul>
     71 <li>
     72 (declaration) </li>
     73 <li>
     74 (dtd stuff) </li>
     75 <li>
     76 Element "PLAY" <ul>
     77 <li>
     78 Element "TITLE" <ul>
     79 <li>
     80 Text "A Midsummer Night's Dream" </li>
     81 </ul>
     82 </li>
     83 </ul>
     84 </li>
     85 </ul>
     86 <p>For this example, we want to print out the title of the play. The text of the title (what we want) is child of the "TITLE" element which is a child of the "PLAY" element.</p>
     87 <p>We want to skip the declaration and dtd, so the method FirstChildElement() is a good choice. The FirstChildElement() of the Document is the "PLAY" Element, the FirstChildElement() of the "PLAY" Element is the "TITLE" Element.</p>
     88 <p><div class="fragment"><div class="line"></div><div class="line">    XMLDocument doc;</div><div class="line">    doc.Parse( xml );</div><div class="line"></div><div class="line">    XMLElement* titleElement = doc.FirstChildElement( <span class="stringliteral">&quot;PLAY&quot;</span> )-&gt;FirstChildElement( <span class="stringliteral">&quot;TITLE&quot;</span> );</div></div><!-- fragment --></p>
     89 <p>We can then use the convenience function GetText() to get the title of the play.</p>
     90 <p><div class="fragment"><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* title = titleElement-&gt;GetText();</div><div class="line">    printf( <span class="stringliteral">&quot;Name of play (1): %s\n&quot;</span>, title );</div></div><!-- fragment --></p>
     91 <p>Text is just another Node in the XML DOM. And in fact you should be a little cautious with it, as text nodes can contain elements.</p>
     92 <pre class="fragment">Consider: A Midsummer Night's &lt;b&gt;Dream&lt;/b&gt;
     93 </pre><p>It is more correct to actually query the Text Node if in doubt:</p>
     94 <p><div class="fragment"><div class="line"></div><div class="line">    XMLText* textNode = titleElement-&gt;FirstChild()-&gt;ToText();</div><div class="line">    title = textNode-&gt;Value();</div><div class="line">    printf( <span class="stringliteral">&quot;Name of play (2): %s\n&quot;</span>, title );</div></div><!-- fragment --></p>
     95 <p>Noting that here we use FirstChild() since we are looking for XMLText, not an element, and ToText() is a cast from a Node to a XMLText. </p>
     96 </div></div><!-- contents -->
     97 <!-- start footer part -->
     98 <hr class="footer"/><address class="footer"><small>
     99 Generated on Tue Nov 6 2018 09:38:26 for TinyXML-2 by &#160;<a href="http://www.doxygen.org/index.html">
    100 <img class="footer" src="doxygen.png" alt="doxygen"/>
    101 </a> 1.8.13
    102 </small></address>
    103 </body>
    104 </html>
    105