【问题标题】:libxml2 and XPath traversing children and siblings in ANSI Clibxml2 和 XPath 在 ANSI C 中遍历子级和兄弟级
【发布时间】:2012-09-21 21:56:14
【问题描述】:

我已经在 Perl 中完成了相当多的 XML 工作,现在我需要在 ANDI C 中为一个项目完成这些工作。这是我用 XML 的 sn-p 编写的代码。我在一定程度上取得了成功,但在获得兄弟姐妹方面遇到了问题,我相信这非常容易,但我就是做不到。有两个函数,一个只是获取节点集(直接从 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 sn-p

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

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

【问题讨论】:

  • 不应该是ANDI MC ... ;-)

标签: c xpath libxml2


【解决方案1】:

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

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

    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-&gt;next也可以指向链接,见how to get these XML elements with libxml2?

然后生孩子? xmlFirstElementChild 和循环 while node-&gt;next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多