Add-Ons & Services

Basic XML Parsing Question..

Please post support and help requests here.

Basic XML Parsing Question..

Postby daroo » 06 Apr 2012, 15:26

This seems like a very basic questoin, but much googling and experimentation has not led to the answer..

I've got an XML file that is organized like this:

Code: Select all
    <subEl>some stuff for first innerEl's subEl</subEl>
    <subEl>some stuff for second innerEl's subEl</subEl>
    <subEl>some stuff for third innerEl's subEl</subEl>

When I run this, what I think is a very simple, main, I get a seg fault - not on the first attempt at getting an innerEl's subEl inner text, but the second time. Interestingly, if I give it the same node path it doesn't seg fault, and I get the correct value (i.e. if I do "innerEl[0]", followed by "innerEl[0]" I get the right value for innerEl[0]/subEl, and if I do "innerEl[1]" followed by "innerEl[1]" I get the right value for innerEl[1]/subEl) but the seg fault occurs the first time I use a different innerEl[i]. I assume this has something to do with the use of the AutoPtr or when the dtor for the Element gets called, but I'm at a loss.. Any help would be greatly appreciated! Thanks!

Code: Select all
int main(
     int argc,
     char *argv[]
  string xmlFname = "test.xml";
  InputSource xmlFile(xmlFname);
  Poco::XML::AutoPtr<Poco::XML::Document> pData;
  DOMParser domParser;
  Poco::XML::AutoPtr<Poco::XML::NodeList> pTopLevelElTags;
  Poco::XML::AutoPtr<Poco::XML::Node> pTopLevelEl;

  pData = domParser.parse(&xmlFile); //exception handling removed for brevity

  pTopLevelElTags = pData->getElementsByTagName("topLevelEl");

  //error checking to make sure exactly one instance removed for brevity

  pTopLevelEl = pTopLevelElTags->item(0);

  int numEntries = 3; //Really get this a different way, but this is fine for testing

  Poco::XML::AutoPtr<Poco::XML::Node> pInnerEl;
  string subElStr;

  pInnerEl = pTopLevelEl->getNodeByPath("/innerEl[0]");
  cout << "Got innerEl 0" << endl;
  subElStr = pInnerEl->getNodeByPath("/subEl")->innerText();
  cout << "  SubEl is: " << subElStr << endl;
  pInnerEl = pTopLevelEl->getNodeByPath("/innerEl[1]");
  cout << "Got innerEl 1" << endl;
  subElStr = pInnerEl->getNodeByPath("/subEl")->innerText(); //<--This line seg faults
  cout << "  SubEl is: " << subElStr << endl;

  cout << "ALL FINISHED" << endl;
  return (0);
Posts: 2
Joined: 06 Apr 2012, 14:51

Re: Basic XML Parsing Question..

Postby daroo » 16 Apr 2012, 16:46

No responses, but one thing I've noticed is that things seem to be 1-based instead of 0-based, so that might have been what was causin the problem. It was a weird manifestation of that if that was the cause. Regardless, I downloaded pugixml since I couldn't get help with poco and it works beautifully and easily, so I never attempted to fix the poco version.
Posts: 2
Joined: 06 Apr 2012, 14:51

Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest