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

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

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;cntnodeNr; cnt++) { title = xmlNodeListGetString(doc, node->nodeTab[cnt]->xmlChildrenNode,1); printf("%d) title= %s
\n",cnt,title); xmlFree(title); } xmlXPathFreeObject(xpathresult); xmlFreeDoc(doc); xmlCleanupParser(); } else { criterr("Xpath failed"); } xmlFreeDoc(doc); criterr("Success"); }

和xml片段

  this is the title this is the link this is the description  

如果我使用类似//item/title的XPath我得到所有标题,但我真正想要的是获取项目然后在node-> nodeNr循环中,能够轻松获得标题,链接和描述,因为我有100个’项目’块,我只是不知道如何轻松地获得该块的孩子或兄弟姐妹。

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

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

  for (cnt=0;cntnodeNr; 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
, link=%s\n",cnt,title,link); xmlFree(title); xmlFree(link); }

titleNode->next也可以指向链接,看看如何用libxml2获取这些XML元素? 。

并得到孩子? xmlFirstElementChild并在node->next循环。