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.w3.org/TR/xpath/

http://www.w3school.com.cn/xpath/index.asp

libxml2用xpath进行查找的更多相关文章

  1. [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]

    场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...

  2. libxml2的xpath检索中文

    ZC: xmlXPathEvalExpression(...) 当 xpath的字符串中 包含中文的时候,返回NULL,暂时不知道该怎么处理了... ZC: 下面是测试的一些代码/文件,留着以后再研究 ...

  3. xpath元素查找提示is not clickable

    1.用xpath可以在chrome找到 $x("//mandatory-config-dialog[@is-show='isShowMandatoryConfig']/div/div[2]/ ...

  4. WebDriver 在使用 CSS Selector 与 XPath 在查找元素时如何取舍

    开发在做Web系统时,用的是css div划分层,使用jQuery 选取元素.

  5. selenium(二)查找定位目标 ID XPATH CSS 操作目标

    简介: 我们只打开一个页面是没有什么意义的. 尽管你可以get很多次. selenium可以做的更多,比如找到百度的搜索框,输入我们要搜索的内容,再用鼠标点击搜索按钮,再把搜索结果提取出来…… 这篇文 ...

  6. 【python】lxml查找属性为指定值的节点

    假设有如下xml在/home/abc.xml位置 <A> <B id=" name="apple"/> <B id=" name= ...

  7. 5.5.8 XPath定位

    1.什么是XPath XPath定位方式是自动化测试定位技术中的必杀技,几乎可以解决所有的定位难题.它是XML Path语言的缩写,主要用于在XML 文档中选择文档中的节点.基于XML树状文档结构,X ...

  8. XML, XPath, Xslt及解析/Parse

    XML及解析/Parse "Programming with libxml2 is like the thrilling embrace of an exotic stranger.&quo ...

  9. 自学Xpath的几个例子

    Xpath可以对XML文件中的信息进行查找,并对XML文件树形结构进行遍历.详细语法请转:http://www.w3school.com.cn/xpath/index.asp 例子:利用在JavaSc ...

随机推荐

  1. C#工具类之素数扩展类

    /// <summary> /// 素数帮忙类 /// 本类是从.net源码 类 internal static class HashHelpers 类里抽取相应的代码 /// https ...

  2. AES/CBC/PKCS5Padding对称加密

    package unit; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.cry ...

  3. windows同时安装python2和python3两个版本

    1.安装python2 下载地址: https://www.python.org/downloads/windows/ 进入页面,下拉,64位系统要选择这个. 下载完成后,一直点击下一步,直到安装完毕 ...

  4. my31_MGR单写模式压测以及对比普通从库记录

    场景MGR单写模式三节点,db46写节点,db47/db48为读节点工具sysbencn.压测15个小时,db46上18线程纯写,12线程oltp混合测试,db48上12线程select在压测2个小时 ...

  5. python 获取子目录下的所有文件的路径

    import os pathss=[] for root, dirs, files in os.walk(tarpath): path = [os.path.join(root, name) for ...

  6. spring-data-elasticsearch使用笔记

    使用spring-data遇到了一些问题,记录一下. spring-data-elasticsearch版本选择 这里有一份官方github上的spring-data-elasticsearch与el ...

  7. 为什么一段时间后网站后台自动退出 php中session过期时间设置

    修改php配置文件中的session.gc_maxlifetime.如果想了解更多session回收机制,继续阅读.(本文环境php5.2) 概述:每一次php请求,会有1/100的概率(默认值)触发 ...

  8. 【shell】《shell学习指南》读书笔记

    一.使用shell脚本 优点:脚本语言能够轻易处理文件与目录之间的对象,如把文件从所有目录拷贝到另一个目录 缺点:效率不如编译型语言 二.简单的脚本 1.查看现在系统有谁登录 $who 2.算出行数 ...

  9. oracle学习篇一:sqlplus常用命令

    1.程序运行--> cmd --> sqlplus 登陆普通用户:scott/brant;普通管理员用户登陆:system/brant;高级管理员用户登陆:1>先切换其他用户:SQL ...

  10. rest-framework框架——APIView和序列化组件

    一.快速实例 Quickstart http://www.cnblogs.com/yuanchenqi/articles/8719520.html restful协议 ---- 一切皆是资源,操作只是 ...