joshua-philpott
joshua-philpott

Reputation: 213

Getting Segmentation Fault with TinyXML

I currently have an XML file which I'm parsing using TinyXML. The top of my XML file look as so :

<Classroom>12
    <ClassName>name</ClassName>
    ...
</Classroom>

I'm attempting to access the text in ClassName. I am currently getting a segmentation fault using this:

TiXmlDocument doc;
doc.LoadFile(file);
TiXmlHandle  handle(&doc);

TiXmlElement * child = handle.FirstChild().FirstChild().ToElement();
cout<<child->GetText();

What am I doing wrong?

Thanks!

Upvotes: 2

Views: 1150

Answers (1)

LMC
LMC

Reputation: 300

  1. The first call to FirstChild() returns a TiXmlElement representing the first <Classroom>
  2. The second call to FirstChild() returns a TiXmlText representing the text "12"
  3. TiXmlText does not override the ToElement() function, therefore using the base class's ToElement() function, which returns NULL.

You can change your code to the following:

TiXmlElement * child = handle.FirstChild("Classroom").FirstChild("ClassName").ToElement();

Upvotes: 1

Related Questions