_example_3.html 6.2 KB

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