xref: /aosp_15_r20/external/tinyxml2/docs/_example_3.html (revision 7485b22521f577cf944e5687361548d8993d8d2c)
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" lang="en-US">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<meta http-equiv="X-UA-Compatible" content="IE=11"/>
6<meta name="generator" content="Doxygen 1.10.0"/>
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<script type="text/javascript" src="clipboard.js"></script>
13<script type="text/javascript" src="cookie.js"></script>
14<link href="search/search.css" rel="stylesheet" type="text/css"/>
15<script type="text/javascript" src="search/searchdata.js"></script>
16<script type="text/javascript" src="search/search.js"></script>
17<link href="doxygen.css" rel="stylesheet" type="text/css" />
18</head>
19<body>
20<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
21<div id="titlearea">
22<table cellspacing="0" cellpadding="0">
23 <tbody>
24 <tr id="projectrow">
25  <td id="projectalign">
26   <div id="projectname">TinyXML-2<span id="projectnumber">&#160;10.0.0</span>
27   </div>
28  </td>
29 </tr>
30 </tbody>
31</table>
32</div>
33<!-- end header part -->
34<!-- Generated by Doxygen 1.10.0 -->
35<script type="text/javascript">
36/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
37var searchBox = new SearchBox("searchBox", "search/",'.html');
38/* @license-end */
39</script>
40<script type="text/javascript" src="menudata.js"></script>
41<script type="text/javascript" src="menu.js"></script>
42<script type="text/javascript">
43/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
44$(function() {
45  initMenu('',true,false,'search.php','Search');
46  $(function() { init_search(); });
47});
48/* @license-end */
49</script>
50<div id="main-nav"></div>
51<!-- window showing the filter options -->
52<div id="MSearchSelectWindow"
53     onmouseover="return searchBox.OnSearchSelectShow()"
54     onmouseout="return searchBox.OnSearchSelectHide()"
55     onkeydown="return searchBox.OnSearchSelectKey(event)">
56</div>
57
58<!-- iframe showing the search results (closed by default) -->
59<div id="MSearchResultsWindow">
60<div id="MSearchResults">
61<div class="SRPage">
62<div id="SRIndex">
63<div id="SRResults"></div>
64<div class="SRStatus" id="Loading">Loading...</div>
65<div class="SRStatus" id="Searching">Searching...</div>
66<div class="SRStatus" id="NoMatches">No Matches</div>
67</div>
68</div>
69</div>
70</div>
71
72</div><!-- top -->
73<div><div class="header">
74  <div class="headertitle"><div class="title">Get information out of XML</div></div>
75</div><!--header-->
76<div class="contents">
77<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>
78<p>(The XML is an excerpt from "dream.xml").</p>
79<div class="fragment"><div class="line"><span class="keywordtype">int</span> example_3()</div>
80<div class="line">{</div>
81<div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* xml =</div>
82<div class="line">        <span class="stringliteral">&quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&quot;</span></div>
83<div class="line">        <span class="stringliteral">&quot;&lt;!DOCTYPE PLAY SYSTEM \&quot;play.dtd\&quot;&gt;&quot;</span></div>
84<div class="line">        <span class="stringliteral">&quot;&lt;PLAY&gt;&quot;</span></div>
85<div class="line">        <span class="stringliteral">&quot;&lt;TITLE&gt;A Midsummer Night&#39;s Dream&lt;/TITLE&gt;&quot;</span></div>
86<div class="line">        <span class="stringliteral">&quot;&lt;/PLAY&gt;&quot;</span>;</div>
87</div><!-- fragment --><p> The structure of the XML file is:</p>
88<ul>
89<li>
90(declaration) </li>
91<li>
92(dtd stuff) </li>
93<li>
94Element "PLAY" <ul>
95<li>
96Element "TITLE" <ul>
97<li>
98Text "A Midsummer Night's Dream" </li>
99</ul>
100</li>
101</ul>
102</li>
103</ul>
104<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>
105<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>
106<div class="fragment"><div class="line"> </div>
107<div class="line">    XMLDocument doc;</div>
108<div class="line">    doc.Parse( xml );</div>
109<div class="line"> </div>
110<div class="line">    XMLElement* titleElement = doc.FirstChildElement( <span class="stringliteral">&quot;PLAY&quot;</span> )-&gt;FirstChildElement( <span class="stringliteral">&quot;TITLE&quot;</span> );</div>
111</div><!-- fragment --><p> We can then use the convenience function GetText() to get the title of the play.</p>
112<div class="fragment"><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* title = titleElement-&gt;GetText();</div>
113<div class="line">    printf( <span class="stringliteral">&quot;Name of play (1): %s\n&quot;</span>, title );</div>
114</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>
115<pre class="fragment">Consider: A Midsummer Night's &lt;b&gt;Dream&lt;/b&gt;
116</pre><p>It is more correct to actually query the Text Node if in doubt:</p>
117<div class="fragment"><div class="line"> </div>
118<div class="line">    XMLText* textNode = titleElement-&gt;FirstChild()-&gt;ToText();</div>
119<div class="line">    title = textNode-&gt;Value();</div>
120<div class="line">    printf( <span class="stringliteral">&quot;Name of play (2): %s\n&quot;</span>, title );</div>
121</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>
122</div></div><!-- contents -->
123</div><!-- PageDoc -->
124<!-- start footer part -->
125<hr class="footer"/><address class="footer"><small>
126Generated on Sat Dec 30 2023 18:02:35 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.10.0
127</small></address>
128</body>
129</html>
130