Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4463523
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: C/C++

2013-01-16 09:57:52

xml文档

[html] view plaincopy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <radios>  
  3.     <radio>  
  4.         <name>Bayernname>  
  5.         <url>url>  
  6.         <classification>  
  7.             <area>usaarea>  
  8.             <style>musicstyle>  
  9.         classification>  
  10.     radio>  
  11.     <radio>  
  12.         <name>DEU-Antenne Bayernname>  
  13.         <url>url>  
  14.     radio>  
  15.     <radio>  
  16.         <name>DEU-Antenne Bayernname>  
  17.         <url>url>  
  18.     radio>  
  19. radios>  
上代码
  1. static xmlXPathObjectPtr getNodeset(xmlDocPtr doc, const xmlChar *xpath)  
  2. {  
  3.     xmlXPathContextPtr context;  
  4.     xmlXPathObjectPtr result;  
  5.     context = xmlXPathNewContext(doc);  
  6.   
  7.     if (context == NULL) {  
  8.         printf("context is NULL\n");  
  9.         return NULL;  
  10.     }  
  11.   
  12.     result = xmlXPathEvalExpression(xpath, context);  
  13.     xmlXPathFreeContext(context);  
  14.     if (result == NULL) {  
  15.         printf("xmlXPathEvalExpression return NULL\n");  
  16.         return NULL;  
  17.     }  
  18.   
  19.     if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {  
  20.         xmlXPathFreeObject(result);  
  21.         printf("nodeset is empty\n");  
  22.         return NULL;  
  23.     }  
  24.   
  25.     return result;  
  26. }  

playlistDoc 为 xmlDocPtr类型.

  1. xmlChar *xpath = BAD_CAST("/radios/radio[name='DEU-Antenne Bayern']");   //关键在这行  
  2. xmlXPathObjectPtr app_result = getNodeset(playlistDoc, xpath);  
  3. if (app_result == NULL)  
  4. {  
  5.     printf("app_result is NULL\n");  
  6.     return;  
  7. }  
  8.   
  9. int i = 0;  
  10. xmlChar *value;  
  11. if(app_result)  
  12. {  
  13.     xmlNodeSetPtr nodeset = app_result->nodesetval;  
  14.     xmlNodePtr cur;  
  15.   
  16.     for (i=0; i < nodeset->nodeNr; i++)  
  17.     {  
  18.         cur = nodeset->nodeTab[i];     
  19.         cur = cur->xmlChildrenNode;  
  20.   
  21.         while (cur != NULL)  
  22.         {  
  23.             if (!xmlStrcmp(cur->name, (const xmlChar *)"name"))  
  24.                 printf("%s\n", ((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));  
  25.             else if (!xmlStrcmp(cur->name, (const xmlChar *)"url"))  
  26.                 printf("%s\n", ((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));  
  27.   
  28.             cur = cur->next;  
  29.         }  
  30.     }  
  31.   
  32.     xmlXPathFreeObject(app_result);  
  33. }  

输出:

DEU-Antenne Bayern

DEU-Antenne Bayern


  1. xmlChar *xpath = BAD_CAST("/radios/radio[name='DEU-Antenne Bayern']");   
改成
  1. xmlChar *xpath = BAD_CAST("/radios/radio[name='DEU-Antenne Bayern' and url='']");  
  2. EU-Antenne Bayern  

输出:

更多xpath的写法可参考

作者:帅得不敢出门   c++哈哈堂:31843264

阅读(6853) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~