libxml的使用(4)--xpath搜索节点树 http://blog.csdn.net/hdutigerkin/article/details/7546912
2015-08-31 08:19
555 查看
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是我们所需要的节点数组。
[cpp] view
plaincopyprint?
xmlXPathObjectPtr ret = NULL;
xmlXPathContextPtr con = NULL;
con = xmlXPathNewContext(doc);
ret = xmlXPathEvalExpression((xmlChar*)expr, con);
xmlXPathFreeContext(con);
这样我们就得到了查询的结果了。expr是查询的条件,tutorial给的例子里,这个条件是“//keyword”,表示找出所有名称为keyword的节点。至于其他的条件,我现在还不知道。
得到了查询的结果,我们就要对结果进行处理。
[cpp] view
plaincopyprint?
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] view
plaincopyprint?
<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
[cpp] view
plaincopyprint?
#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;
}
输出结果为:
[html] view
plaincopyprint?
link address:www.baidu.com
link address:www.google.com
相关文章推荐
- libxml的使用(2)--读取节点属性 http://blog.csdn.net/hdutigerkin/article/details/7546907
- libxml的使用(1)--读取xml http://blog.csdn.net/hdutigerkin/article/details/7546899
- Leetcode #217 Contains Duplicate
- HDU 4458 Shoot the Airplane(计算几何 判断点是否在n边形内)
- ResponderChain note
- 【转载】MFC的Main函数跑哪去了
- TurboMail邮件系统与利郎服装集团合作
- OC之非ARC环境下循环retain问题
- Aircrack-ng之Aireplay-ng命令
- 几种常用RAID的特性
- trait里的默认函数会为每个类型拷贝一份
- Apache之——命令行启动出现Failed to open the Apache2.2 Service解决方案
- 软raid-mdadm命令
- RAID常用等级介绍
- XML XML常见的操作 http://blog.csdn.net/pbymw8iwm/article/details/7899156
- HDU1022 Train Problem I
- UltraISO制作U盘启动盘
- UVA 442 二十 Matrix Chain Multiplication
- HDU 4458 Shoot the Airplane (2012年杭州赛区现场赛F题)
- poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)