libxml2和XPath在ANSI C中遍历子节点和兄弟节点

时间:2012-09-21 21:56:15

标签: c xpath libxml2

我在Perl中做了很多XML工作,现在我需要在ANDI C中为项目做一些事情。这是我用XML片段编写的代码。我已经获得了学位的成功,但我遇到兄弟姐妹的问题,我相信这很容易,但我无法得到它。有两个函数,一个只是简单地获取节点集(直接从xmlsoft.org复制)。第二个功能是我的。

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");
}

和xml代码段

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

如果我使用像//item/title这样的XPath,我会获得所有标题,但我真正想要的是获取项目,然后在node-&gt; nodeNr循环中,能够获得标题,链接和很容易描述,因为我有100个'项目'块,我只是不确定如何轻松地获得该块的孩子或兄弟姐妹。

1 个答案:

答案 0 :(得分:4)

使用xmlNextElementSibling。如何找到它?转到Tree API,搜索兄弟

现在这也是你的循环链接。

    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也可以指向该链接,请参阅how to get these XML elements with libxml2?

生孩子? xmlFirstElementChild并循环node->next