libxml: persistence of xmlXPathObject.nodeset->nodeTab elements

H

Hoi-Polloi

Hi all

I want to use an xpath query to get a set of xmlNodePtr , but I don't
want to keep the xmlXPathObject around.

See the function below. I want to:
* make xpath query and get an xmlXPathObjectPtr back
* get a pointer (np) to the XPathObjectPtr->nodesetval->nodeTab[0]
* free XPathObjectPtr
* return the pointer np ... but I'm not sure if np is still valid
after deleting the
XPathObjectPtr

Can someone help me puzzle it out?

xmlNodePtr get_first_result_from_xpath(xmlDocPtr doc, xmlChar *xpath)
{
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
xmlNodePtr np;

context = xmlXPathNewContext(doc);
if (context == NULL) {
return NULL;
}
result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if (result == NULL) {
return NULL;
}
if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
xmlXPathFreeObject(result);
return NULL;
}

nodeset = result->nodesetval;
if (nodeset->nodeNr)
{
np = nodeset->nodeTab[0];
xmlXPathFreeObject(result);
//
// QUESTION: is np still valid after I delete "result"?
//
return np;
}

return NULL;
}

H-P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top