1
0

_example-3.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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: 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">5.0.1</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">
  35. var 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. <!-- iframe showing the search results (closed by default) -->
  53. <div id="MSearchResultsWindow">
  54. <iframe src="javascript:void(0)" frameborder="0"
  55. name="MSearchResults" id="MSearchResults">
  56. </iframe>
  57. </div>
  58. </div><!-- top -->
  59. <div class="header">
  60. <div class="headertitle">
  61. <div class="title">Get information out of XML </div> </div>
  62. </div><!--header-->
  63. <div class="contents">
  64. <div class="textblock"> 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>
  65. <p>(The XML is an excerpt from "dream.xml").</p>
  66. <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>
  67. <p>The structure of the XML file is:</p>
  68. <ul>
  69. <li>
  70. (declaration) </li>
  71. <li>
  72. (dtd stuff) </li>
  73. <li>
  74. Element "PLAY" <ul>
  75. <li>
  76. Element "TITLE" <ul>
  77. <li>
  78. Text "A Midsummer Night's Dream" </li>
  79. </ul>
  80. </li>
  81. </ul>
  82. </li>
  83. </ul>
  84. <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>
  85. <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>
  86. <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>
  87. <p>We can then use the convenience function GetText() to get the title of the play.</p>
  88. <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>
  89. <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>
  90. <pre class="fragment">Consider: A Midsummer Night's &lt;b&gt;Dream&lt;/b&gt;
  91. </pre><p>It is more correct to actually query the Text Node if in doubt:</p>
  92. <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>
  93. <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>
  94. </div></div><!-- contents -->
  95. <!-- start footer part -->
  96. <hr class="footer"/><address class="footer"><small>
  97. Generated on Sat Jul 15 2017 19:39:36 for TinyXML-2 by &#160;<a href="http://www.doxygen.org/index.html">
  98. <img class="footer" src="doxygen.png" alt="doxygen"/>
  99. </a> 1.8.13
  100. </small></address>
  101. </body>
  102. </html>