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 ...
随机推荐
- async中series的实现 javascript构件
//同步流程 var series=function(arr){ function async(i){ arr[i](function(){ if(1+i<arr.length){ async( ...
- 【总结】ettercap工具之DNS劫持
在arp欺骗成功基础上开始进行dns劫持,,, 1.修改/etc/ettercap/etter.dns文件内容,如下图所示. 将所有域名解析到192.168.1.250地址上,,, 2.etterca ...
- java将文本写入本地硬盘
注意:首先要在E盘创建qaz.txt文本文件.然后执行代码写入. public static void main(String[] args) { SecurityCodeUtils scu = ne ...
- (转)[Shell]tr命令详解
原文:http://blog.csdn.net/sunnyyoona/article/details/52986893 1. 用途 tr,translate的简写,主要用于压缩重复字符,删除文件中的控 ...
- 牛客网Java刷题知识点之数组、链表、哈希表、 红黑二叉树
不多说,直接上干货! 首先来说一个非常形象的例子,来说明下数组和链表. 上体育课的时候,老师说:你们站一队,每个人记住自己是第几个,我喊到几,那个人就举手,这就是数组. 老师说,你们每个人记住自己前面 ...
- C++11并发编程:原子操作atomic
一:概述 项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式. 为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一 ...
- [转]C# - JSON详解
本文转自:http://www.cnblogs.com/QLJ1314/p/3862583.html 最近在做微信开发时用到了一些json的问题,就是把微信返回回来的一些json数据做一些处理,但是之 ...
- JavaScript实现StringBuffer
function StringBuffer() { this._strings = new Array(); } StringBuffer.prototype.Append = function(_s ...
- C# ADO.NET面向对象想法
我认为的面向对象就是把各种问题拆分开来 逐一解决, 我想的是先是数据库,到底有什么, 然后新建一个类,类里面先是private的私有的,但是可以有无数个可以连接private的pubilc的属性 可 ...
- java.util.regex.Pattern正则表达式写验证器示例
import java.util.regex.Pattern; /** * 校验器:利用正则表达式校验邮箱.手机号等 * */ public class Validator { /** * 正则表达式 ...