• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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.9.1"/>
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">9.0.0</span>
26   </div>
27  </td>
28 </tr>
29 </tbody>
30</table>
31</div>
32<!-- end header part -->
33<!-- Generated by Doxygen 1.9.1 -->
34<script type="text/javascript">
35/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
36var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
37/* @license-end */
38</script>
39<script type="text/javascript" src="menudata.js"></script>
40<script type="text/javascript" src="menu.js"></script>
41<script type="text/javascript">
42/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
43$(function() {
44  initMenu('',true,false,'search.php','Search');
45  $(document).ready(function() { init_search(); });
46});
47/* @license-end */</script>
48<div id="main-nav"></div>
49<!-- window showing the filter options -->
50<div id="MSearchSelectWindow"
51     onmouseover="return searchBox.OnSearchSelectShow()"
52     onmouseout="return searchBox.OnSearchSelectHide()"
53     onkeydown="return searchBox.OnSearchSelectKey(event)">
54</div>
55
56<!-- iframe showing the search results (closed by default) -->
57<div id="MSearchResultsWindow">
58<iframe src="javascript:void(0)" frameborder="0"
59        name="MSearchResults" id="MSearchResults">
60</iframe>
61</div>
62
63</div><!-- top -->
64<div class="PageDoc"><div class="header">
65  <div class="headertitle">
66<div class="title">Get information out of XML </div>  </div>
67</div><!--header-->
68<div class="contents">
69<div class="textblock"><p> 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>
70<p>(The XML is an excerpt from "dream.xml").</p>
71<div class="fragment"><div class="line"><span class="keywordtype">int</span> example_3()</div>
72<div class="line">{</div>
73<div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* xml =</div>
74<div class="line">        <span class="stringliteral">&quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&quot;</span></div>
75<div class="line">        <span class="stringliteral">&quot;&lt;!DOCTYPE PLAY SYSTEM \&quot;play.dtd\&quot;&gt;&quot;</span></div>
76<div class="line">        <span class="stringliteral">&quot;&lt;PLAY&gt;&quot;</span></div>
77<div class="line">        <span class="stringliteral">&quot;&lt;TITLE&gt;A Midsummer Night&#39;s Dream&lt;/TITLE&gt;&quot;</span></div>
78<div class="line">        <span class="stringliteral">&quot;&lt;/PLAY&gt;&quot;</span>;</div>
79</div><!-- fragment --><p> The structure of the XML file is:</p>
80<ul>
81<li>
82(declaration) </li>
83<li>
84(dtd stuff) </li>
85<li>
86Element "PLAY" <ul>
87<li>
88Element "TITLE" <ul>
89<li>
90Text "A Midsummer Night's Dream" </li>
91</ul>
92</li>
93</ul>
94</li>
95</ul>
96<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>
97<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>
98<div class="fragment"><div class="line"> </div>
99<div class="line">    XMLDocument doc;</div>
100<div class="line">    doc.Parse( xml );</div>
101<div class="line"> </div>
102<div class="line">    XMLElement* titleElement = doc.FirstChildElement( <span class="stringliteral">&quot;PLAY&quot;</span> )-&gt;FirstChildElement( <span class="stringliteral">&quot;TITLE&quot;</span> );</div>
103</div><!-- fragment --><p> We can then use the convenience function GetText() to get the title of the play.</p>
104<div class="fragment"><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* title = titleElement-&gt;GetText();</div>
105<div class="line">    printf( <span class="stringliteral">&quot;Name of play (1): %s\n&quot;</span>, title );</div>
106</div><!-- fragment --><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>
107<pre class="fragment">Consider: A Midsummer Night's &lt;b&gt;Dream&lt;/b&gt;
108</pre><p>It is more correct to actually query the Text Node if in doubt:</p>
109<div class="fragment"><div class="line"> </div>
110<div class="line">    XMLText* textNode = titleElement-&gt;FirstChild()-&gt;ToText();</div>
111<div class="line">    title = textNode-&gt;Value();</div>
112<div class="line">    printf( <span class="stringliteral">&quot;Name of play (2): %s\n&quot;</span>, title );</div>
113</div><!-- fragment --><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>
114</div></div><!-- contents -->
115</div><!-- PageDoc -->
116<!-- start footer part -->
117<hr class="footer"/><address class="footer"><small>
118Generated on Sun Jun 6 2021 17:10:05 for TinyXML-2 by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
119</small></address>
120</body>
121</html>
122