Welcome to mirror list, hosted at ThFree Co, Russian Federation.

_example_3.html « docs - github.com/leethomason/tinyxml2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: abbc896f691e5a8627c47b52f92aeeb60dd54390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TinyXML-2: Get information out of XML</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">TinyXML-2
   &#160;<span id="projectnumber">8.0.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Get information out of XML </div>  </div>
</div><!--header-->
<div class="contents">
<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>
<p>(The XML is an excerpt from "dream.xml").</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>The structure of the XML file is:</p>
<ul>
<li>
(declaration) </li>
<li>
(dtd stuff) </li>
<li>
Element "PLAY" <ul>
<li>
Element "TITLE" <ul>
<li>
Text "A Midsummer Night's Dream" </li>
</ul>
</li>
</ul>
</li>
</ul>
<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>
<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>
<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>We can then use the convenience function GetText() to get the title of the play.</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>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>
<pre class="fragment">Consider: A Midsummer Night's &lt;b&gt;Dream&lt;/b&gt;
</pre><p>It is more correct to actually query the Text Node if in doubt:</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>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>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Sat Jun 13 2020 19:01:40 for TinyXML-2 by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.18
</small></address>
</body>
</html>