Re: [xml] libxml2 Child Indexing?



Thanks to John Dennis and Rob Richards, I solved the problem. It's a little more work than I hoped, but it's better than using a while loop. Code is as follows for future reference on the interwebs:

rootNode = xmlDocGetRootElement(doc);

//  Create new context to support XPath.
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL) {
    fprintf(stderr,"Error: unable to create new XPath context\n");
    xmlFreeDoc(doc);
    return(-1);
}

sprintf(someString, "/%s/*[2]", rootNode->name);
xpathObj = xmlXPathEvalExpression(someString, xpathCtx);
nodeSet=xpathObj->nodesetval;
secondChildNode=nodeSet->nodeTab[0];

printf("Node name: %s", secondChildNode->name);


-UN.

On Thu, Apr 16, 2009 at 2:02 PM, Rob Richards <rrichards cdatazone org> wrote:
Uzumaki Naruto wrote:
Hi all,

I'm using libxml2, and I've been scouring the documentation for a function to retrieve the handle of a child node that is not either the first/last (ie: 2nd child / 5).

What I'm looking for is, for example:

rootNode = xmlDocGetRootElement(doc);
secondChildNode = xmlFoo(rootNode , 2);
printf("Node name: %s", secondChildNode->name);

I am currently doing:

rootNode = xmlDocGetRootElement(doc);
firstChildNode = xmlFirstElementChild(rootNode);
secondChildNode = xmlNextElementSibling(nextChildNode);
printf("Node name: %s", secondChildNode->name);
If you dont want to do it manually then you need to use XPath to retrieve by position.

Rob



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]