_example_3.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 */
  37. var 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. <!-- iframe showing the search results (closed by default) -->
  58. <div id="MSearchResultsWindow">
  59. <div id="MSearchResults">
  60. <div class="SRPage">
  61. <div id="SRIndex">
  62. <div id="SRResults"></div>
  63. <div class="SRStatus" id="Loading">Loading...</div>
  64. <div class="SRStatus" id="Searching">Searching...</div>
  65. <div class="SRStatus" id="NoMatches">No Matches</div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. </div><!-- top -->
  71. <div><div class="header">
  72. <div class="headertitle"><div class="title">Get information out of XML</div></div>
  73. </div><!--header-->
  74. <div class="contents">
  75. <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>
  76. <p>(The XML is an excerpt from "dream.xml").</p>
  77. <div class="fragment"><div class="line"><span class="keywordtype">int</span> example_3()</div>
  78. <div class="line">{</div>
  79. <div class="line"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* xml =</div>
  80. <div class="line"> <span class="stringliteral">&quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&quot;</span></div>
  81. <div class="line"> <span class="stringliteral">&quot;&lt;!DOCTYPE PLAY SYSTEM \&quot;play.dtd\&quot;&gt;&quot;</span></div>
  82. <div class="line"> <span class="stringliteral">&quot;&lt;PLAY&gt;&quot;</span></div>
  83. <div class="line"> <span class="stringliteral">&quot;&lt;TITLE&gt;A Midsummer Night&#39;s Dream&lt;/TITLE&gt;&quot;</span></div>
  84. <div class="line"> <span class="stringliteral">&quot;&lt;/PLAY&gt;&quot;</span>;</div>
  85. </div><!-- fragment --><p> The structure of the XML file is:</p>
  86. <ul>
  87. <li>
  88. (declaration) </li>
  89. <li>
  90. (dtd stuff) </li>
  91. <li>
  92. Element "PLAY" <ul>
  93. <li>
  94. Element "TITLE" <ul>
  95. <li>
  96. Text "A Midsummer Night's Dream" </li>
  97. </ul>
  98. </li>
  99. </ul>
  100. </li>
  101. </ul>
  102. <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>
  103. <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>
  104. <div class="fragment"><div class="line"> </div>
  105. <div class="line"> XMLDocument doc;</div>
  106. <div class="line"> doc.Parse( xml );</div>
  107. <div class="line"> </div>
  108. <div class="line"> XMLElement* titleElement = doc.FirstChildElement( <span class="stringliteral">&quot;PLAY&quot;</span> )-&gt;FirstChildElement( <span class="stringliteral">&quot;TITLE&quot;</span> );</div>
  109. </div><!-- fragment --><p> We can then use the convenience function GetText() to get the title of the play.</p>
  110. <div class="fragment"><div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span>* title = titleElement-&gt;GetText();</div>
  111. <div class="line"> printf( <span class="stringliteral">&quot;Name of play (1): %s\n&quot;</span>, title );</div>
  112. </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>
  113. <pre class="fragment">Consider: A Midsummer Night's &lt;b&gt;Dream&lt;/b&gt;
  114. </pre><p>It is more correct to actually query the Text Node if in doubt:</p>
  115. <div class="fragment"><div class="line"> </div>
  116. <div class="line"> XMLText* textNode = titleElement-&gt;FirstChild()-&gt;ToText();</div>
  117. <div class="line"> title = textNode-&gt;Value();</div>
  118. <div class="line"> printf( <span class="stringliteral">&quot;Name of play (2): %s\n&quot;</span>, title );</div>
  119. </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>
  120. </div></div><!-- contents -->
  121. </div><!-- PageDoc -->
  122. <!-- start footer part -->
  123. <hr class="footer"/><address class="footer"><small>
  124. Generated 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
  125. </small></address>
  126. </body>
  127. </html>