I'm trying to loop trough every single element in my XML file to see if any of their content matches the string specified (in this case the string is Earth)
However, it for some reason can't find the element Earth, however if the element in the actual XML file isn't inside a element other than the root element it can find it.
So it can, for example, find Sun and Moon.
Here's the XML file :
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Sun>Sun.</Sun>
<Moon>Moon.</Moon>
<Planets>
<Earth>Im the earth.</Earth>
<Mars>Im chocolate.</Mars>
<Uranus>Hurr-Durr</Uranus>
<SomeOtherPlanet>???</SomeOtherPlanet>
</Planets>
<AShip>Just a UFO passing by...</AShip>
</Root>
And here's what my function that finds the element looks like :
std::string XMLFile::GetStringAttribute(std::string element)
{
if (mErr == true)
{
std::cout << "XML : Something went wrong, could not get attribute." << std::endl;
return "XML_UNKNOWN_ERROR";
}
tinyxml2::XMLNode* rootElement = mDoc.FirstChildElement("Root");
if (rootElement == nullptr)
{
std::cout << "XML : No Root element could be found inside file : " << mFileName.c_str() << std::endl;
return "FAILED_TO_LOAD_ROOT_ELEMENT";
}
for (tinyxml2::XMLElement* elem = rootElement->FirstChildElement(element.c_str()); elem != NULL; elem = rootElement->NextSiblingElement(element.c_str()))
{
return elem->GetText();
}
std::cout << "XML : Failed to find element : " << element.c_str() << ", inside file : " << mFileName.c_str() << std::endl;
return "FAILED_TO_FIND_ELEMENT";
}
And here's how I call that function :
std::cout << file.GetStringAttribute("Earth") << std::endl;
I also have this function to load the file, (I know this part is working) ;
bool XMLFile::LoadFile(std::string filepath)
{
mFileName = filepath;
if (mDoc.LoadFile(filepath.c_str()) < 0)
{
std::cout << "XML : Failed to load XML file : " << filepath.c_str() << std::endl;
mErr = true;
return false;
}
}
And that function is called like this :
XMLFile file;
file.LoadFile("Data/XML_Data/test.xml");
XMLFile is the class which holds the 2 functions LoadFile() and GetStringAttribute()
And mDoc is just a XMLDocument variable :
tinyxml2::XMLDocument mDoc;
I think this is all the information needed to recreate the problem, unsure what I'm doing wrong.