libxml2和XPath的穿越子女和兄弟姐妹在ANSI C [英] libxml2 and XPath traversing children and siblings in ANSI C

查看:122
本文介绍了libxml2和XPath的穿越子女和兄弟姐妹在ANSI C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的在Perl XML的东西公平一点,现在我需要做的是岸堤下一个项目。这里的code我使用XML的一个片段中写道。我已经成功在一定程度上,但我有与得到兄弟姐妹的问题,我相信它的超级容易,但我只是无法得到它。有两个功能,一是简单地获取节点集(直接从xmlsoft.org复制)。第二个功能是我的。

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

和XML片段

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

如果我使用像 //项目/标题我得到的所有冠军,但我真正想要的是获得该项目,然后在一个于节点的XPath> nodeNr循环,能拿到冠军,链接和描述很容易,因为我有'项目'100块的,我只是不知道如何轻松地获得该块的子女或兄弟姐妹。

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.

推荐答案

使用 xmlNextElementSibling 。怎样才能找到它?转到树API ,搜索的兄弟

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

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-&gt;接下来也可以由点链接,看到的如何得到这些XML元素与libxml2的?

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

和让孩子们? xmlFirstElementChild 并循环而&于节点gt;接下来

这篇关于libxml2和XPath的穿越子女和兄弟姐妹在ANSI C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆