<!-- iframe showing the search results (closed by default) -->
<divid="MSearchResultsWindow">
<iframesrc="javascript:void(0)"frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<divclass="header">
<divclass="headertitle">
<divclass="title">Get information out of XML </div></div>
</div><!--header-->
<divclass="contents">
<divclass="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>
<p>(The XML is an excerpt from "dream.xml").</p>
<p><divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span> example_3()</div><divclass="line">{</div><divclass="line"><spanclass="keyword">static</span><spanclass="keyword">const</span><spanclass="keywordtype">char</span>* xml =</div><divclass="line"><spanclass="stringliteral">"<?xml version=\"1.0\"?>"</span></div><divclass="line"><spanclass="stringliteral">"<!DOCTYPE PLAY SYSTEM \"play.dtd\">"</span></div><divclass="line"><spanclass="stringliteral">"<PLAY>"</span></div><divclass="line"><spanclass="stringliteral">"<TITLE>A Midsummer Night's Dream</TITLE>"</span></div><divclass="line"><spanclass="stringliteral">"</PLAY>"</span>;</div></div><!-- fragment --></p>
<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>
<p><divclass="fragment"><divclass="line"></div><divclass="line"> XMLDocument doc;</div><divclass="line"> doc.Parse( xml );</div><divclass="line"></div><divclass="line"> XMLElement* titleElement = doc.FirstChildElement( <spanclass="stringliteral">"PLAY"</span> )->FirstChildElement( <spanclass="stringliteral">"TITLE"</span> );</div></div><!-- fragment --></p>
<p>We can then use the convenience function GetText() to get the title of the play.</p>
<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>
<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>
<p><divclass="fragment"><divclass="line"></div><divclass="line"> XMLText* textNode = titleElement->FirstChild()->ToText();</div><divclass="line"> title = textNode->Value();</div><divclass="line"> printf( <spanclass="stringliteral">"Name of play (2): %s\n"</span>, title );</div></div><!-- fragment --></p>
<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>