<divclass="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>
</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><!-- fragment --><p> We can then use the convenience function GetText() to get the title of the play.</p>
<divclass="fragment"><divclass="line"><spanclass="keyword">const</span><spanclass="keywordtype">char</span>* title = titleElement->GetText();</div>
<divclass="line"> printf( <spanclass="stringliteral">"Name of play (1): %s\n"</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>
<preclass="fragment">Consider: A Midsummer Night's <b>Dream</b>
</pre><p>It is more correct to actually query the Text Node if in doubt:</p>
<divclass="line"> title = textNode->Value();</div>
<divclass="line"> printf( <spanclass="stringliteral">"Name of play (2): %s\n"</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>
Generated on Sat Dec 30 2023 18:02:35 for TinyXML-2 by <ahref="https://www.doxygen.org/index.html"><imgclass="footer"src="doxygen.svg"width="104"height="31"alt="doxygen"/></a> 1.10.0