TinyXML2 - Trouble Getting Started

538 Views Asked by At

I'm just getting started with the XML parsing library, but I'm having trouble getting started. (The learning curve, I guess) More specifically, I'm dealing with an exception when trying to get an element and use it. Right now I have this line:

tinyxml2::XMLElement *firstEvent = doc.FirstChildElement("EVENTS")->FirstChildElement();

Which throws a memory exception. For some reason, I can't show my XML code, but the structure is

file->EVENTS->event->some more content

So what this seems like to me is that the parser can't access the "event" element. What could be causing this? What am I doing wrong?! Any help would be very nice!

1

There are 1 best solutions below

5
seccpur On

Assuming this is your xml content:

<events>
  <event> "Move" <\event>
  <event> "Walk" <\event>
  <event> "Run" <\event>
<\events>

Test if the XmlElement is not NULL before iteration to avoid throwing exception: C++ code snippet can look like this:

XmlElement* elem = doc.FirstChildElement("events");
if(elem != NULL)
{
   for (XmlElement* e = elem->FirstChildElement("event"); e != NULL; e = e->NextSiblingElement("event"))
   {
        const char *c = e->GetText(); // if its an attrib use e->Attribute("event-type");
        /* more  */
   }
}