TinyXML-2
9.0.0
|
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.
(The XML is an excerpt from "dream.xml").
The structure of the XML file is:
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.
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.
We can then use the convenience function GetText() to get the title of the play.
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.
Consider: A Midsummer Night's <b>Dream</b>
It is more correct to actually query the Text Node if in doubt:
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.