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: Read attributes and text information.</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">
35var 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">Read attributes and text information. </div>  </div>
64</div><!--header-->
65<div class="contents">
66<div class="textblock"><p>There are fundamentally 2 ways of writing a key-value pair into an XML file. (Something that's always annoyed me about XML.) Either by using attributes, or by writing the key name into an element and the value into the text node wrapped by the element. Both approaches are illustrated in this example, which shows two ways to encode the value "2" into the key "v":</p>
67<p><div class="fragment"><div class="line"><span class="keywordtype">bool</span> example_4()</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;information&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;   &lt;attributeApproach v=&#39;2&#39; /&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;   &lt;textApproach&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;       &lt;v&gt;2&lt;/v&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;   &lt;/textApproach&gt;&quot;</span></div><div class="line">        <span class="stringliteral">&quot;&lt;/information&gt;&quot;</span>;</div></div><!-- fragment --></p>
68<p>TinyXML-2 has accessors for both approaches.</p>
69<p>When using an attribute, you navigate to the XMLElement with that attribute and use the QueryIntAttribute() group of methods. (Also QueryFloatAttribute(), etc.)</p>
70<p><div class="fragment"><div class="line">    XMLElement* attributeApproachElement = doc.FirstChildElement()-&gt;FirstChildElement( <span class="stringliteral">&quot;attributeApproach&quot;</span> );</div><div class="line">    attributeApproachElement-&gt;QueryIntAttribute( <span class="stringliteral">&quot;v&quot;</span>, &amp;v0 );</div></div><!-- fragment --></p>
71<p>When using the text approach, you need to navigate down one more step to the XMLElement that contains the text. Note the extra FirstChildElement( "v" ) in the code below. The value of the text can then be safely queried with the QueryIntText() group of methods. (Also QueryFloatText(), etc.)</p>
72<p><div class="fragment"><div class="line">    XMLElement* textApproachElement = doc.FirstChildElement()-&gt;FirstChildElement( <span class="stringliteral">&quot;textApproach&quot;</span> );</div><div class="line">    textApproachElement-&gt;FirstChildElement( <span class="stringliteral">&quot;v&quot;</span> )-&gt;QueryIntText( &amp;v1 );</div></div><!-- fragment --> </p>
73</div></div><!-- contents -->
74<!-- start footer part -->
75<hr class="footer"/><address class="footer"><small>
76Generated on Tue Nov 6 2018 09:38:26 for TinyXML-2 by &#160;<a href="http://www.doxygen.org/index.html">
77<img class="footer" src="doxygen.png" alt="doxygen"/>
78</a> 1.8.13
79</small></address>
80</body>
81</html>
82