libxml2 and XPath traversing children and siblings

2019-07-03 23:57发布

问题:

I have done a fair bit of XML stuff in Perl and now I need to do it in ANDI C for a project. Here's the code I wrote with a snippet of the XML. I have had success to a degree, but am having problems with getting siblings, I am sure it's super easy but I just can't get it. There is two functions, one that simply gets the node set (copied directly from xmlsoft.org). The second function is mine.

xmlXPathObjectPtr getnodeset (xmlDocPtr doc, xmlChar *xpath){

    xmlXPathContextPtr context;
    xmlXPathObjectPtr result;

    context = xmlXPathNewContext(doc);
    if (context == NULL) {
        printf("Error in xmlXPathNewContext\n");
        return NULL;
    }

    result = xmlXPathEvalExpression(xpath, context);
    xmlXPathFreeContext(context);

    if (result == NULL) {
        printf("Error in xmlXPathEvalExpression\n");
        return NULL;
    }

    if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
        xmlXPathFreeObject(result);
                printf("No result\n");
        return NULL;
    }

    return result;
}

    void reader(xmlDocPtr xmlDoc, char *xpath)
{

    xmlXPathObjectPtr xpathresult;
    xmlNodeSetPtr node;
    xmlNodeSetPtr node2;
    xmlChar *title;

    int cnt;

    // parse feed in memory to xml object
    doc = xmlReadMemory(xmlDoc,strlen(xmlDoc),"noname.xml",NULL,0);

    if (!doc) criterr("Error parsing xml document");

    // get xpath node set (ttn retrieves the value from the token table)
    xpathresult = getnodeset(doc, ( xmlChar * ) xpath);

    if (xpathresult) {
        node = xpathresult->nodesetval;
        printf("Content-type: text/html\n\n");

        for (cnt=0;cnt<node->nodeNr; cnt++) {

            title = xmlNodeListGetString(doc, node->nodeTab[cnt]->xmlChildrenNode,1);

            printf("%d) title= %s<br/>\n",cnt,title);
            xmlFree(title);         
        }

        xmlXPathFreeObject(xpathresult);
        xmlFreeDoc(doc);
        xmlCleanupParser();

    } else {
        criterr("Xpath failed");
    }

    xmlFreeDoc(doc);

    criterr("Success");
}

and the xml snippet

<item>
  <title>this is the title</title>
  <link>this is the link</link>
  <description>this is the description</description>
</item>

if I use an XPath like //item/title I get all the titles, but what I really want is to get the item and then in the node->nodeNr loop, be able to get the title, link and description easily as I have 100's of 'item' blocks, I'm just not sure how to get the children or siblings of that block easily.

回答1:

Use xmlNextElementSibling. How does one locate it? Go to Tree API, search for sibling.

And this is your loop now getting also the link.

    for (cnt=0;cnt<node->nodeNr; cnt++) {
        xmlNodePtr titleNode = node->nodeTab[cnt];
        // titleNode->next gives empty text element, so better:
        xmlNodePtr linkNode = xmlNextElementSibling(titleNode);

        title = xmlNodeListGetString(doc, titleNode->xmlChildrenNode,1);
        link = xmlNodeListGetString(doc, linkNode->xmlChildrenNode,1);

        printf("%d) title= %s<br/>, link=%s\n",cnt,title,link);
        xmlFree(title);         
        xmlFree(link);
    }

titleNode->next may also be made to point the link, see how to get these XML elements with libxml2?.

And getting children? xmlFirstElementChild and loop while node->next.



标签: c xpath libxml2