heidi k
heidi k

Reputation: 1

Check if an XML node exists, using Tinyxml

I am using tinyXml for parsing an XML file in C++. Could anyone please tell me how do I check to see if a node (parent/child/next sibling) exists or not. Below are the only nodes present in the xml file I'm working on.

TiXmlElement* Instrmt = TrdCaptRpt->FirstChildElement();
TiXmlElement* Undly = Instrmt->NextSiblingElement();
TiXmlElement* Amt = Undly->NextSiblingElement();
TiXmlElement* RptSide = Amt->NextSiblingElement();
TiXmlElement* Pty = RptSide->FirstChildElement();

if any of the nodes above is missing in the sequence then the program aborts with a segmentation fault.

Could anyone please help.

Thanks

Upvotes: 0

Views: 5831

Answers (2)

Pete
Pete

Reputation: 1303

TiXml provides the TiXMlHandle class to take care of checking for NULL, so it should sort out the segmentaiton faults. You still need to check the existence of the node at the end of the chain.

Upvotes: 1

Tony The Lion
Tony The Lion

Reputation: 63200

You can use the const TiXmlNode* TiXmlNode::FirstChild ( const char * value ) const function of the TiXmlNode class and check if the resulting TiXmlNode* is NULL or not.

TiXmlNode* child = mynode->FirstChild();

if (child != NULL)
{
  //A child exists....
}

For Parent you have a similar function. You can find the documentation here.

I hope this helps.

Upvotes: 1

Related Questions