I've got an XML file that is organized like this:
- Code: Select all
<topLevelEl>
<innerEl>
<subEl>some stuff for first innerEl's subEl</subEl>
</innerEl>
<innerEl>
<subEl>some stuff for second innerEl's subEl</subEl>
</innerEl>
<innerEl>
<subEl>some stuff for third innerEl's subEl</subEl>
</innerEl>
</topLevelEl>
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);
}





