libxml2用xpath进行查找
xml文档
<?xml version="1.0" encoding="UTF-8"?>
<radios>
<radio>
<name>Bayern</name>
<url>http://mp3.webradio.antenne.de:80</url>
<classification>
<area>usa</area>
<style>music</style>
</classification>
</radio>
<radio>
<name>DEU-Antenne Bayern</name>
<url>http://mp3.webradio.antenne.de:80</url>
</radio>
<radio>
<name>DEU-Antenne Bayern</name>
<url>http://test</url>
</radio>
</radios>
代码
static xmlXPathObjectPtr getNodeset(xmlDocPtr doc, const xmlChar *xpath)
{
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext(doc); if (context == NULL) {
printf("context is NULL\n");
return NULL;
} result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if (result == NULL) {
printf("xmlXPathEvalExpression return NULL\n");
return NULL;
} if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
xmlXPathFreeObject(result);
printf("nodeset is empty\n");
return NULL;
} return result;
}
playlistDoc 为 xmlDocPtr类型.
xmlChar *xpath = BAD_CAST("/radios/radio[name='DEU-Antenne Bayern']"); //关键在这行
xmlXPathObjectPtr app_result = getNodeset(playlistDoc, xpath);
if (app_result == NULL)
{
printf("app_result is NULL\n");
return;
}
int i = ;
xmlChar *value;
if(app_result)
{
xmlNodeSetPtr nodeset = app_result->nodesetval;
xmlNodePtr cur;
for (i=; i < nodeset->nodeNr; i++)
{
cur = nodeset->nodeTab[i];
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if (!xmlStrcmp(cur->name, (const xmlChar *)"name"))
printf("%s\n", ((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
else if (!xmlStrcmp(cur->name, (const xmlChar *)"url"))
printf("%s\n", ((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
cur = cur->next;
}
}
xmlXPathFreeObject(app_result);
}
输出:
DEU-Antenne Bayern
http://mp3.webradio.antenne.de:80
DEU-Antenne Bayern
http://test
更多xpath的写法可参考
http://www.w3school.com.cn/xpath/index.asp
libxml2用xpath进行查找的更多相关文章
- [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]
场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...
- libxml2的xpath检索中文
ZC: xmlXPathEvalExpression(...) 当 xpath的字符串中 包含中文的时候,返回NULL,暂时不知道该怎么处理了... ZC: 下面是测试的一些代码/文件,留着以后再研究 ...
- xpath元素查找提示is not clickable
1.用xpath可以在chrome找到 $x("//mandatory-config-dialog[@is-show='isShowMandatoryConfig']/div/div[2]/ ...
- WebDriver 在使用 CSS Selector 与 XPath 在查找元素时如何取舍
开发在做Web系统时,用的是css div划分层,使用jQuery 选取元素.
- selenium(二)查找定位目标 ID XPATH CSS 操作目标
简介: 我们只打开一个页面是没有什么意义的. 尽管你可以get很多次. selenium可以做的更多,比如找到百度的搜索框,输入我们要搜索的内容,再用鼠标点击搜索按钮,再把搜索结果提取出来…… 这篇文 ...
- 【python】lxml查找属性为指定值的节点
假设有如下xml在/home/abc.xml位置 <A> <B id=" name="apple"/> <B id=" name= ...
- 5.5.8 XPath定位
1.什么是XPath XPath定位方式是自动化测试定位技术中的必杀技,几乎可以解决所有的定位难题.它是XML Path语言的缩写,主要用于在XML 文档中选择文档中的节点.基于XML树状文档结构,X ...
- XML, XPath, Xslt及解析/Parse
XML及解析/Parse "Programming with libxml2 is like the thrilling embrace of an exotic stranger.&quo ...
- 自学Xpath的几个例子
Xpath可以对XML文件中的信息进行查找,并对XML文件树形结构进行遍历.详细语法请转:http://www.w3school.com.cn/xpath/index.asp 例子:利用在JavaSc ...
随机推荐
- vm 中安装 CentOS7
第三步:安装ISO文件 1.在vm下,文件,新建虚拟机 在我的机算机中,选中刚命名的CentOS7,右键,属性 2.开启虚拟机 PS: 打开虚拟机之后,提示了一个小错误,LZ根据错误提示,到BIOS里 ...
- 选择IM云服务供应商
选择IM云服务供应商,其实最重要是有三个因素:费用.技术稳定性.以及后续运维服务. 对于不少创业公司来讲,可能需要找到成本和稳定性的最佳平衡点.目前国内不少IM云服务产品都推出了免费服务项目或者一定期 ...
- 对avalonjs的研究
<!DOCTYPE html> <html> <head> <title>第一个avalon项目</title> <meta char ...
- 【实战】Oracle注入总结
小结: Union联合查询: order by 定字段 and 1=2 union select null,null..... from dual 然后一个一个去判断字段类型,方法如下 and 1=2 ...
- 认识HTML中表格、列表标签以及表单控件
前端之HTML,CSS(二) HTML标签 列表标签 无序列表:闭标签,由<ul><li></li>...</ul>组合而成,效果成纵向列表.格式:&l ...
- mysql数据库知识
学而时习之,不亦说乎! --<论语> 数据库所有操作的总结. 1.mysql的数据库服务为mysqld.exe windo ...
- MongoDB 配置服务
参考网址:https://jingyan.baidu.com/article/d5c4b52b906bafda560dc591.html 1.MongoDB 有一个不方便,需要配置服务,默认启动,否则 ...
- Redis在windows下的配置
Redis在windows下的配置(在windows-64下安装redis,请参考微软redis的github:https://github.com/MSOpenTech/redis/releases ...
- jquery、js判断复选框是否选中
js: if (document.getElementById("checkboxID").checked) { alert("checkobx is checked&q ...
- pat00-自测2. 素数对猜想 (20)
00-自测2. 素数对猜想 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们定义 dn 为:dn ...