How does one go about getting the text from the nodes and subnodes in TinyXML2?
The XMLPrinter class seems to do what I need, but it does not print the text properly.
My XML:
<div>The quick brown <b>fox</b> jumps over the <i>lazy</i> dog.</div>
My class which extends the XMLPrinter class:
class XMLTextPrinter : public XMLPrinter {
virtual bool VisitEnter (const XMLDocument &) { return true; }
virtual bool VisitExit (const XMLDocument &) { return true; }
virtual bool VisitEnter (const XMLElement &e, const XMLAttribute *) {
auto text = e.GetText();
if(text) {
std::cout << text;
}
return true;
}
virtual bool VisitExit (const XMLElement &e) { return true; }
virtual bool Visit (const XMLDeclaration &) { return true; }
virtual bool Visit (const XMLText &e) { return true; }
virtual bool Visit (const XMLComment &) { return true; }
virtual bool Visit (const XMLUnknown &) { return true; }
};
My code:
XMLDocument document;
document.Parse(..., ...);
auto elem = ...;
XMLTextPrinter printer;
elem->Accept(&printer);
The output:
The quick brown foxlazy
Why is it ignoring all text which come after the <b>
and <i>
elements? How can I solve this? Also, the XMLPrinter class properly prints it out with the tags, but I do not want the tags.