1*7485b225SElliott Hughes<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2*7485b225SElliott Hughes<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> 3*7485b225SElliott Hughes<head> 4*7485b225SElliott Hughes<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> 5*7485b225SElliott Hughes<meta http-equiv="X-UA-Compatible" content="IE=11"/> 6*7485b225SElliott Hughes<meta name="generator" content="Doxygen 1.10.0"/> 7*7485b225SElliott Hughes<meta name="viewport" content="width=device-width, initial-scale=1"/> 8*7485b225SElliott Hughes<title>TinyXML-2: Get information out of XML</title> 9*7485b225SElliott Hughes<link href="tabs.css" rel="stylesheet" type="text/css"/> 10*7485b225SElliott Hughes<script type="text/javascript" src="jquery.js"></script> 11*7485b225SElliott Hughes<script type="text/javascript" src="dynsections.js"></script> 12*7485b225SElliott Hughes<script type="text/javascript" src="clipboard.js"></script> 13*7485b225SElliott Hughes<script type="text/javascript" src="cookie.js"></script> 14*7485b225SElliott Hughes<link href="search/search.css" rel="stylesheet" type="text/css"/> 15*7485b225SElliott Hughes<script type="text/javascript" src="search/searchdata.js"></script> 16*7485b225SElliott Hughes<script type="text/javascript" src="search/search.js"></script> 17*7485b225SElliott Hughes<link href="doxygen.css" rel="stylesheet" type="text/css" /> 18*7485b225SElliott Hughes</head> 19*7485b225SElliott Hughes<body> 20*7485b225SElliott Hughes<div id="top"><!-- do not remove this div, it is closed by doxygen! --> 21*7485b225SElliott Hughes<div id="titlearea"> 22*7485b225SElliott Hughes<table cellspacing="0" cellpadding="0"> 23*7485b225SElliott Hughes <tbody> 24*7485b225SElliott Hughes <tr id="projectrow"> 25*7485b225SElliott Hughes <td id="projectalign"> 26*7485b225SElliott Hughes <div id="projectname">TinyXML-2<span id="projectnumber"> 10.0.0</span> 27*7485b225SElliott Hughes </div> 28*7485b225SElliott Hughes </td> 29*7485b225SElliott Hughes </tr> 30*7485b225SElliott Hughes </tbody> 31*7485b225SElliott Hughes</table> 32*7485b225SElliott Hughes</div> 33*7485b225SElliott Hughes<!-- end header part --> 34*7485b225SElliott Hughes<!-- Generated by Doxygen 1.10.0 --> 35*7485b225SElliott Hughes<script type="text/javascript"> 36*7485b225SElliott Hughes/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */ 37*7485b225SElliott Hughesvar searchBox = new SearchBox("searchBox", "search/",'.html'); 38*7485b225SElliott Hughes/* @license-end */ 39*7485b225SElliott Hughes</script> 40*7485b225SElliott Hughes<script type="text/javascript" src="menudata.js"></script> 41*7485b225SElliott Hughes<script type="text/javascript" src="menu.js"></script> 42*7485b225SElliott Hughes<script type="text/javascript"> 43*7485b225SElliott Hughes/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */ 44*7485b225SElliott Hughes$(function() { 45*7485b225SElliott Hughes initMenu('',true,false,'search.php','Search'); 46*7485b225SElliott Hughes $(function() { init_search(); }); 47*7485b225SElliott Hughes}); 48*7485b225SElliott Hughes/* @license-end */ 49*7485b225SElliott Hughes</script> 50*7485b225SElliott Hughes<div id="main-nav"></div> 51*7485b225SElliott Hughes<!-- window showing the filter options --> 52*7485b225SElliott Hughes<div id="MSearchSelectWindow" 53*7485b225SElliott Hughes onmouseover="return searchBox.OnSearchSelectShow()" 54*7485b225SElliott Hughes onmouseout="return searchBox.OnSearchSelectHide()" 55*7485b225SElliott Hughes onkeydown="return searchBox.OnSearchSelectKey(event)"> 56*7485b225SElliott Hughes</div> 57*7485b225SElliott Hughes 58*7485b225SElliott Hughes<!-- iframe showing the search results (closed by default) --> 59*7485b225SElliott Hughes<div id="MSearchResultsWindow"> 60*7485b225SElliott Hughes<div id="MSearchResults"> 61*7485b225SElliott Hughes<div class="SRPage"> 62*7485b225SElliott Hughes<div id="SRIndex"> 63*7485b225SElliott Hughes<div id="SRResults"></div> 64*7485b225SElliott Hughes<div class="SRStatus" id="Loading">Loading...</div> 65*7485b225SElliott Hughes<div class="SRStatus" id="Searching">Searching...</div> 66*7485b225SElliott Hughes<div class="SRStatus" id="NoMatches">No Matches</div> 67*7485b225SElliott Hughes</div> 68*7485b225SElliott Hughes</div> 69*7485b225SElliott Hughes</div> 70*7485b225SElliott Hughes</div> 71*7485b225SElliott Hughes 72*7485b225SElliott Hughes</div><!-- top --> 73*7485b225SElliott Hughes<div><div class="header"> 74*7485b225SElliott Hughes <div class="headertitle"><div class="title">Get information out of XML</div></div> 75*7485b225SElliott Hughes</div><!--header--> 76*7485b225SElliott Hughes<div class="contents"> 77*7485b225SElliott Hughes<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> 78*7485b225SElliott Hughes<p>(The XML is an excerpt from "dream.xml").</p> 79*7485b225SElliott Hughes<div class="fragment"><div class="line"><span class="keywordtype">int</span> example_3()</div> 80*7485b225SElliott Hughes<div class="line">{</div> 81*7485b225SElliott Hughes<div class="line"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* xml =</div> 82*7485b225SElliott Hughes<div class="line"> <span class="stringliteral">"<?xml version=\"1.0\"?>"</span></div> 83*7485b225SElliott Hughes<div class="line"> <span class="stringliteral">"<!DOCTYPE PLAY SYSTEM \"play.dtd\">"</span></div> 84*7485b225SElliott Hughes<div class="line"> <span class="stringliteral">"<PLAY>"</span></div> 85*7485b225SElliott Hughes<div class="line"> <span class="stringliteral">"<TITLE>A Midsummer Night's Dream</TITLE>"</span></div> 86*7485b225SElliott Hughes<div class="line"> <span class="stringliteral">"</PLAY>"</span>;</div> 87*7485b225SElliott Hughes</div><!-- fragment --><p> The structure of the XML file is:</p> 88*7485b225SElliott Hughes<ul> 89*7485b225SElliott Hughes<li> 90*7485b225SElliott Hughes(declaration) </li> 91*7485b225SElliott Hughes<li> 92*7485b225SElliott Hughes(dtd stuff) </li> 93*7485b225SElliott Hughes<li> 94*7485b225SElliott HughesElement "PLAY" <ul> 95*7485b225SElliott Hughes<li> 96*7485b225SElliott HughesElement "TITLE" <ul> 97*7485b225SElliott Hughes<li> 98*7485b225SElliott HughesText "A Midsummer Night's Dream" </li> 99*7485b225SElliott Hughes</ul> 100*7485b225SElliott Hughes</li> 101*7485b225SElliott Hughes</ul> 102*7485b225SElliott Hughes</li> 103*7485b225SElliott Hughes</ul> 104*7485b225SElliott Hughes<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> 105*7485b225SElliott Hughes<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> 106*7485b225SElliott Hughes<div class="fragment"><div class="line"> </div> 107*7485b225SElliott Hughes<div class="line"> XMLDocument doc;</div> 108*7485b225SElliott Hughes<div class="line"> doc.Parse( xml );</div> 109*7485b225SElliott Hughes<div class="line"> </div> 110*7485b225SElliott Hughes<div class="line"> XMLElement* titleElement = doc.FirstChildElement( <span class="stringliteral">"PLAY"</span> )->FirstChildElement( <span class="stringliteral">"TITLE"</span> );</div> 111*7485b225SElliott Hughes</div><!-- fragment --><p> We can then use the convenience function GetText() to get the title of the play.</p> 112*7485b225SElliott Hughes<div class="fragment"><div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span>* title = titleElement->GetText();</div> 113*7485b225SElliott Hughes<div class="line"> printf( <span class="stringliteral">"Name of play (1): %s\n"</span>, title );</div> 114*7485b225SElliott Hughes</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> 115*7485b225SElliott Hughes<pre class="fragment">Consider: A Midsummer Night's <b>Dream</b> 116*7485b225SElliott Hughes</pre><p>It is more correct to actually query the Text Node if in doubt:</p> 117*7485b225SElliott Hughes<div class="fragment"><div class="line"> </div> 118*7485b225SElliott Hughes<div class="line"> XMLText* textNode = titleElement->FirstChild()->ToText();</div> 119*7485b225SElliott Hughes<div class="line"> title = textNode->Value();</div> 120*7485b225SElliott Hughes<div class="line"> printf( <span class="stringliteral">"Name of play (2): %s\n"</span>, title );</div> 121*7485b225SElliott Hughes</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> 122*7485b225SElliott Hughes</div></div><!-- contents --> 123*7485b225SElliott Hughes</div><!-- PageDoc --> 124*7485b225SElliott Hughes<!-- start footer part --> 125*7485b225SElliott Hughes<hr class="footer"/><address class="footer"><small> 126*7485b225SElliott HughesGenerated on Sat Dec 30 2023 18:02:35 for TinyXML-2 by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.10.0 127*7485b225SElliott Hughes</small></address> 128*7485b225SElliott Hughes</body> 129*7485b225SElliott Hughes</html> 130