tinyxml2
tinyxml2 copied to clipboard
XML comment node prevent GetText()
#include <tinyxml2.h>
#include <iostream>
#include <cassert>
int main(){
// normal case
{
tinyxml2::XMLDocument doc;
doc.Parse("<a>ABC</a>");
auto a = doc.FirstChildElement();
assert(a->GetText() == std::string("ABC")); // ok
}
// comment prevent GetText()
{
tinyxml2::XMLDocument doc;
doc.Parse("<a><!--ABC-->XYZ</a>"); // comment out "ABC"
auto a = doc.FirstChildElement();
//assert(a->GetText() == std::string("XYZ")); // expected return "XYZ"
assert(a->GetText() == nullptr); // but, return nullptr
}
}
GetText() : https://github.com/leethomason/tinyxml2/blob/7.0.1/tinyxml2.cpp#L1580
FirstChild() is comment node. so, GetText() return nullptr. XML comment node prevent GetText(). it is hard to predict.
Getting text using gettext () in the current implementation leads to an unfortunate accident