libxml的使用(4)--xpath搜索节点树 http://blog.csdn.net/hdutigerkin/article/details/7546912
来源:互联网 发布:杀手2影子阴谋mac 编辑:程序博客网 时间:2024/06/18 14:26
libxml的使用(4)--xpath搜索节点树
2012-05-08 16:49 1323人阅读 评论(0) 收藏 举报
nullhtmlapixml
在libxml的tutorial中介绍了一种用关键字查找节点的方法,这种方法将使用打xpath系列API。由于我才刚刚接触libxml,所以我对xpath的认识也仅仅是在tutorial提供的功能之内了。废话少说,直接进入整体。
我们在操作xml文件是经常需要根据特定的条件查找一系列的节点,为了实现这样的功能,我们需要一个xmlXPathContextPtr和一个expression。我们调用xmlXPathEvalExpression函数来得到一个xmlXPathObjectPtr指针,这个指针包含了一个xmlNodeSetPtr,其中有一个变量nodeTab是我们所需要的节点数组。
- xmlXPathObjectPtr ret = NULL;
- xmlXPathContextPtr con = NULL;
- con = xmlXPathNewContext(doc);
- ret = xmlXPathEvalExpression((xmlChar*)expr, con);
- xmlXPathFreeContext(con);
这样我们就得到了查询的结果了。expr是查询的条件,tutorial给的例子里,这个条件是“//keyword”,表示找出所有名称为keyword的节点。至于其他的条件,我现在还不知道。
得到了查询的结果,我们就要对结果进行处理。
- if(NULL == ret) {
- fprintf(stderr, "eval func error\n");
- exit(1);
- }
- if(xmlXPathNodeSetIsEmpty(ret->nodesetval)){
- fprintf(stderr, "node set empty\n");
- xmlXPathFreeObject(ret);
- exit(1);
- }
- xmlNodeSetPtr nodeset = ret->nodesetval;
- int i;
- for(i = 0; i < nodeset->nodeNr; i ++) {
- //handle the node
- }
- xmlXPathFreeObject(ret);
下面是一个程序的实例。用于提取出网页中的链接:
web.html
- <html>
- <head>
- <title>web</title>
- </head>
- <body>
- <a href="www.baidu.com">baidu</a>
- <a href="www.google.com">Google</a>
- </body>
- </html>
link.c
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <libxml/parser.h>
- #include <libxml/xpath.h>
- static xmlDocPtr
- getDocPtr(char* docname) {
- xmlDocPtr doc = NULL;
- xmlKeepBlanksDefault(0);
- doc = xmlParseFile(docname);
- if(NULL == doc) {
- fprintf(stderr, "document cannot be parsed!\n");
- exit(1);
- }
- return doc;
- }
- static xmlXPathObjectPtr
- getXPathObjectPtr(xmlDocPtr doc, xmlChar* xpath_exp) {
- xmlXPathObjectPtr result;
- xmlXPathContextPtr context;
- context = xmlXPathNewContext(doc);
- result = xmlXPathEvalExpression((const xmlChar*)xpath_exp, context);
- xmlXPathFreeContext(context);
- if(NULL == result) {
- fprintf(stderr, "eval expression error!\n");
- return NULL;
- }
- if(xmlXPathNodeSetIsEmpty(result->nodesetval)) {
- fprintf(stderr, "empty node set!\n");
- xmlXPathFreeObject(result);
- return NULL;
- }
- return result;
- }
- int main() {
- char* docname = "web.html";
- xmlDocPtr doc = NULL;
- xmlXPathObjectPtr xpath_obj = NULL;
- xmlNodeSetPtr nodeset = NULL;
- xmlChar* xpath_exp = (xmlChar*)"//a";
- xmlChar* uri;
- doc = getDocPtr(docname);
- xpath_obj = getXPathObjectPtr(doc, xpath_exp);
- if(NULL != xpath_obj) {
- nodeset = xpath_obj->nodesetval;
- int i = 0;
- for(i = 0; i < nodeset->nodeNr; i ++) {
- uri = xmlGetProp(nodeset->nodeTab[i],(const xmlChar*)"href");
- printf("link address:%s\n",uri);
- xmlFree(uri);
- }
- xmlXPathFreeObject(xpath_obj);
- }
- xmlFreeDoc(doc);
- xmlCleanupParser();
- return 1;
- }
输出结果为:
- link address:www.baidu.com
- link address:www.google.com
0 0
- libxml的使用(4)--xpath搜索节点树 http://blog.csdn.net/hdutigerkin/article/details/7546912
- libxml的使用(2)--读取节点属性 http://blog.csdn.net/hdutigerkin/article/details/7546907
- libxml的使用(1)--读取xml http://blog.csdn.net/hdutigerkin/article/details/7546899
- libxml读取xml文件的其他方法 http://blog.csdn.net/hdutigerkin/article/details/7547014
- xmlInitParser和xmlCleanupParser使用详解 http://blog.csdn.net/hdutigerkin/article/details/7548596
- epoll详细工作原理 http://blog.csdn.net/hdutigerkin/article/details/7517390
- libxml的使用(4)--xpath搜索节点树
- libxml的使用(4)--xpath搜索节点树
- libxml的使用(4)--xpath搜索节点树
- libxml的使用(4)--xpath搜索节点树
- fastjson的使用(转自:http://blog.csdn.net/wx_962464/article/details/37612861)
- xml节点和元素的关系 http://blog.csdn.net/chaiyu2002/article/details/5967186
- solr搭建企业级应用搜索(转自http://blog.csdn.net/zdshare/article/details/17756121)
- selenium使用中遇到的问题,转自:http://blog.csdn.net/funi16/article/details/9036753
- C++使用localtime函数需要注意的地方http://blog.csdn.net/shellching/article/details/8114266
- UNIX SIGIO信号的使用http://blog.csdn.net/msda/article/details/8090748
- 使用信号实现异步通知机制的例子 http://blog.csdn.net/buaa_shang/article/details/9103155
- C++中智能指针的设计和使用 http://blog.csdn.net/hackbuteer1/article/details/7561235
- 程序员的逗比瞬间
- libxml的使用(1)--读取xml http://blog.csdn.net/hdutigerkin/article/details/7546899
- YARN源码分析(一)-----ApplicationMaster
- libxml的使用(2)--读取节点属性 http://blog.csdn.net/hdutigerkin/article/details/7546907
- libxml的使用(3)--编辑节点
- libxml的使用(4)--xpath搜索节点树 http://blog.csdn.net/hdutigerkin/article/details/7546912
- libxml读取xml文件的其他方法 http://blog.csdn.net/hdutigerkin/article/details/7547014
- xmlInitParser和xmlCleanupParser使用详解 http://blog.csdn.net/hdutigerkin/article/details/7548596
- ADO.NET五大对象
- epoll详细工作原理 http://blog.csdn.net/hdutigerkin/article/details/7517390
- 网易面试题(JAVA)
- Android编程规范
- SCI审稿过程中的几种状态
- 惊喜的八月,充满能量的英语