Dominick
Dominick

Reputation: 321

Parsing XML files: Root Nodes has no child nodes

I never worked with XML and XML parsers and i wanted to parse a COLLADA document for model animation with OpenGL.

I am using the tinyxml2 parser and it seems that I am doing something wrong with that.

XMLDocument _doc;
_doc.LoadFile(path.c_str());

XMLNode* pRoot = _doc.FirstChild();

XMLNode* pElement =  pRoot->FirstChildElement("library_geometries");

I am working with Xcode and in debugging mode I can see, that pElement is NULL also that pRoot has got no child nodes.

Upvotes: 0

Views: 387

Answers (1)

stanthomas
stanthomas

Reputation: 1181

In tinyxml2 everything is a node, not just elements. So _doc.FirstChild() is unhelpful as it's taking you to a node before the <COLLADA> element (probably an attribute in the XML header). What you want is the first child element in the document, i.e. <COLLADA> followed by the first <library_geometries> element below it. Try this:

#include "tinyxml2.h"
using namespace tinyxml2;
int main()
{
   XMLDocument doc;
   doc.LoadFile ("collada.xml");

   auto colladaElement = doc .FirstChildElement();
   auto lib_geomElement = colladaElement -> FirstChildElement("library_geometries");

   return 0;
}

And, if you want more of a C++11/14 experience you could try my tinyxml2 extension which reduces the above to:

#include "tixml2ex.h"
int main()
{
   tinyxml2::XMLDocument doc;
   doc.LoadFile ("collada.xml");
   auto lib_geomElement = find_element (doc, "COLLADA/library_geometries");

   return 0;
}

Upvotes: 1

Related Questions