XML解析

来源:互联网 发布:淘宝的淘气值有什么用 编辑:程序博客网 时间:2024/06/13 07:39

原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819

XML是一种非常重要的文件格式,由于C++对XML的支持非常完善,cocos2d-x选择XML作为主要的文件存储格式。在cocos2d-x中集成了libxml2来解析XML数据。

 

定义一个用于解析的类,这个类继承CCSAXDelegator和CCObject,然后实现CCSAXDelegator的纯虚函数。

 

[cpp] view plaincopy
  1. #ifndef XMLANALYSIS_H_H  
  2. #define XMLANALYSIS_H_H  
  3. #include "cocos2d.h"  
  4. #include <string>  
  5. using namespace cocos2d;  
  6. using namespace std;  
  7. class XMLAnalysis:public CCSAXDelegator,public CCObject//继承CCSAXDelegator,覆盖纯虚函数  
  8. {  
  9. public:  
  10.     XMLAnalysis(const char* data,unsigned int length);//解析数据  
  11.     XMLAnalysis(const char* filename);                    //解析文件  
  12.     ~XMLAnalysis(void);  
  13.     virtual void startElement(void *ctx, const char *name, const char **atts);//开始标签  
  14.     virtual void endElement(void *ctx, const char *name);                     //标签结束  
  15.     virtual void textHandler(void *ctx, const char *s, int len);              //标签的内容  
  16.     string rootname;  
  17. };  
  18. #endif  


源文件

[cpp] view plaincopy
  1. #include "XMLAnalysis.h"  
  2.   
  3.   
  4. XMLAnalysis::XMLAnalysis(const char* data,unsigned int length)  
  5. {  
  6.     CCSAXParser parser;           //定义解析  
  7.     if(parser.init("UTF-8")==0)   //不是utf-8就不解析了  
  8.     {  
  9.         CCLog("please use utf-8");  
  10.         return;  
  11.     }  
  12.     parser.setDelegator(this);   //设置解析的对象,这样就会调用解析的方法  
  13.     parser.parse(data,length);   
  14. }  
  15. XMLAnalysis::XMLAnalysis(const char* filename)  
  16. {  
  17.     CCSAXParser parser;  
  18.     if(parser.init("UTF-8")==0)  
  19.     {  
  20.         CCLog("please use utf-8");  
  21.         return;  
  22.     }  
  23.     parser.setDelegator(this);  
  24.     parser.parse(filename);  
  25. }  
  26.   
  27.   
  28. XMLAnalysis::~XMLAnalysis(void)  
  29. {  
  30.       
  31. }  
  32. void XMLAnalysis::startElement(void *ctx, const char *name, const char **atts)  
  33. {  
  34.       
  35.     if(strcmp(name,"root"))  
  36.     {  
  37.     rootname=name;  
  38.     }  
  39. }  
  40. void XMLAnalysis::textHandler(void *ctx, const char *s, int len)  
  41. {  
  42.     string str=string(s,0,len);  
  43.       
  44.     if(rootname!="")  
  45.     {  
  46.         CCLog("%s",str.c_str());      
  47.     }  
  48. }  
  49.   
  50. void XMLAnalysis::endElement(void *ctx, const char *name)  
  51. {  
  52.       
  53.     rootname="";  
  54. }  


这样每次就打印出标签的内容。

下面使用CURL联网,从网络获取XML来解析,至于CURL联网的配置,请查看我的上一篇文章【Cocos2d-x游戏引擎开发笔记(24)】CURL实现get和post联网。定义一个静态变量std::string str;用于保存网络数据

[cpp] view plaincopy
  1. static string str;  


在源文件中:

[cpp] view plaincopy
  1. std::string HelloWorld::str;  


在按钮的点击事件中,开始联网:

[cpp] view plaincopy
  1. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  2. {  
  3.     str="";  
  4.     //usePost();  
  5.     useGet();  
  6.     XMLAnalysis *p=new XMLAnalysis(str.c_str(),strlen(str.c_str()));//解析数据  
  7.     //XMLAnalysis *p=new XMLAnalysis("xmltemp.xml");                //解析文件  
  8.     delete p;  
  9. }  


这里联网是阻塞的,所以调用useGet之后,全部数据获得之后才开始解析的。下面是useGet的实现:

[cpp] view plaincopy
  1. void HelloWorld::useGet()  
  2. {  
  3.     CURL *curl;  
  4.     CURLcode res;  
  5.       
  6.     curl=curl_easy_init();  
  7.     if(curl)  
  8.     {  
  9.         curl_easy_setopt(curl,CURLOPT_URL,"http://dota.uuu9.com/rss.xml");  
  10.         curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writehtml);  
  11.         res=curl_easy_perform(curl);  
  12.         if(res!=CURLE_OK)  
  13.         {  
  14.             CCLog("联网超时 %i",res);  
  15.         }  
  16.           
  17.         curl_easy_cleanup(curl);  
  18.     }  
  19.     else  
  20.     {  
  21.         CCLog("curl is null");  
  22.         return ;  
  23.     }  
  24. }  


在writehtml函数中,将返回的数据全部保存到str中

[cpp] view plaincopy
  1. size_t HelloWorld::writehtml(uint8_t* ptr,size_t size,size_t number,void *stream)  
  2. {  
  3.     char* buffer=new char[16*1024+1];  
  4.     memset(buffer,0,16*1024+1);  
  5.     sprintf(buffer,"%s",ptr);  
  6.     CCLog("data length is %d",strlen(buffer));  
  7.     CCLog("size*number is %i",size*number);  
  8.     //CCLog("%s",ptr);  
  9.     str=str+buffer;  
  10.     delete []buffer;  
  11.     return size*number;//这里一定要放回实际返回的字节数  
  12. }  


这里我测试过,最大返回16k的数据,C Style字符串规定,最后一个字节是\0,所以多出一个字节存放\0。下面是从游久返回的数据,已经全部解析出来了:

[cpp] view plaincopy
  1.     
  2. DOTA  
  3. 专题站 - 游久(U9)网 -DOTA6.66 - DOTA6.67 - DOTA6.66B - AI - DOTA - 地图下载  
  4. http://dota.uuu9.com/  
  5. 最专业的DOTA专题站,提供最全面最新的地图下载,大型英雄专题,英雄模拟器,录像,战报,攻略。最新,最快,最全,一切尽在DOTA.UUU9.COM  
  6. zh-CN  
  7. http://dota.uuu9.com/rss.xml  
  8.   
  9.      
  10. [视频] 小满食尸鬼:笑容如何从脸上消失的  
  11. http://dota.uuu9.com/201306/101447.shtml  
  12. 您还没有安装flash播放器,请点击这里安装  
  13. 2013-6-19 9:23:24  
  14. http://dota.uuu9.com/201306/101447.shtml  
  15.   
  16.      
  17. [视频] 黑曼巴出品:曼巴时刻top10第十二弹  
  18. http://dota.uuu9.com/201306/101446.shtml  
  19. 您还没有安装flash播放器,请点击这里安装  
  20. 2013-6-19 9:17:19  
  21. http://dota.uuu9.com/201306/101446.shtml  
  22.   
  23.      
  24. [视频] 凯文教你打Carry:团战之王 幽鬼  
  25. http://dota.uuu9.com/201306/101445.shtml  
  26. 您还没有安装flash播放器,请点击这里安装  
  27. 2013-6-19 9:07:31  
  28. http://dota.uuu9.com/201306/101445.shtml  
  29.   
  30.      
  31. [视频] WGT现场pis采访:暂不考虑征战DOTA2  
  32. http://dota.uuu9.com/201306/101444.shtml  
  33. 您还没有安装flash播放器,请点击这里安装  
  34. 2013-6-18 18:49:37  
  35. http://dota.uuu9.com/201306/101444.shtml  
  36.   
  37.      
  38. [视频] Ks夫妻二人套路黑:拍拍与lion的故事  
  39. http://dota.uuu9.com/201306/101443.shtml  
  40. 您还没有安装flash播放器,请点击这里安装  
  41. 2013-6-18 18:25:39  
  42. http://dota.uuu9.com/201306/101443.shtml  
  43.   
  44.      
  45. [视频] 抱抱熊出品:DOTA意识流特比拼VOL.38  
  46. http://dota.uuu9.com/201306/101438.shtml  
  47. 您还没有安装flash播放器,请点击这里安装  
  48. 2013-6-18 17:02:51  
  49. http://dota.uuu9.com/201306/101438.shtml  
  50.   
  51.      
  52. [视频] 丶没有bi要出品:操作集锦 第六期  
  53. http://dota.uuu9.com/201306/101434.shtml  
  54. 您还没有安装flash播放器,请点击这里安装  
  55. 2013-6-18 11:15:52  
  56. http://dota.uuu9.com/201306/101434.shtml  
  57.   
  58.      
  59. [视频] 抱抱熊出品:DOTA集锦每周TOP7第55期  
  60. http://dota.uuu9.com/201306/101433.shtml  
  61. 您还没有安装flash播放器,请点击这里安装  
  62. 2013-6-18 11:02:16  
  63. http://dota.uuu9.com/201306/101433.shtml  
  64.   
  65.      
  66. [视频] ECL小组赛:Orange vs Rstars回顾  
  67. http://dota2.uuu9.com/201306/446153.shtml  
  68.   
  69. 2013-6-18 10:36:35  
  70. http://dota2.uuu9.com/201306/446153.shtml  
  71.   
  72.      
  73. [视频] 专访GTL西北赛区线下赛嘉宾ZSMJ  
  74. http://dota2.uuu9.com/201306/446152.shtml  
  75.   
  76. 2013-6-18 10:11:22  
  77. http://dota2.uuu9.com/201306/446152.shtml  
  78.   
  79.      
  80. [视频] Alienware Cup LGD vs DK两场回顾  
  81. http://dota2.uuu9.com/201306/446139.shtml  
  82.   
  83. 2013-6-18 9:45:41  
  84. http://dota2.uuu9.com/201306/446139.shtml  
  85.   
  86.      
  87. [视频] 游久DOTA精彩集锦第一期:影魔时刻  
  88. http://dota.uuu9.com/201306/101427.shtml  
  89. 您还没有安装flash播放器,请点击这里安装  
  90. 2013-6-18 8:43:42  
  91. http://dota.uuu9.com/201306/101427.shtml  
  92.   
  93.      
  94. [图赏] WGT颁奖图赏 Pis,Maybe高举奖杯  
  95. http://dota.uuu9.com/201306/101425.shtml  
  96.   
  97. 2013-6-17 17:15:49  
  98. http://dota.uuu9.com/201306/101425.shtml  
  99.   
  100.      
  101. [视频] 牛蛙DOTA1/2拍拍熊:超神霸气的杀戮  
  102. http://dota.uuu9.com/201306/101423.shtml  
  103. 您还没有安装flash播放器,请点击这里安装  
  104. 2013-6-17 15:45:31  
  105. http://dota.uuu9.com/201306/101423.shtml  
  106.   
  107.      
  108. [视频] 水友制作娱乐视频:蛋疼镜头集锦124期  
  109. http://dota.uuu9.com/201306/101419.shtml  
  110. 您还没有安装flash播放器,请点击这里安装  
  111. 2013-6-17 14:54:53  
  112. http://dota.uuu9.com/201306/101419.shtml  
  113.   
  114.      
  115. [视频] ZEAM出品教父制作:捣塔冷笑话第五弹  
  116. http://dota.uuu9.com/201306/101417.shtml  
  117. 您还没有安装flash播放器,请点击这里安装  
  118. 2013-6-17 14:41:16  
  119. http://dota.uuu9.com/201306/101417.shtml  
  120.   
  121.      
  122. [视频] 浅雪dota:教大家打先手(纯属娱乐)  
  123. http://dota.uuu9.com/201306/101421.shtml  
  124. 您还没有安装flash播放器,请点击这里安装  
  125. 2013-6-17 12:15:43  
  126. http://dota.uuu9.com/201306/101421.shtml  
  127.   
  128.      
  129. [视频] WGT总决赛Greedy战胜LJBF视频回顾  
  130. http://dota.uuu9.com/201306/101413.shtml  
  131. WGT线下赛总决赛 Greedy VS LJBF  
  132. 2013-6-17 11:45:02  
  133. http://dota.uuu9.com/201306/101413.shtml  
  134.   
  135.      
  136. [视频] 彩笔制作:DOTA反杀集锦Top10第64期  
  137. http://dota.uuu9.com/201306/101412.shtml  
  138. 您还没有安装flash播放器,请点击这里安装  
  139. 2013-6-17 11:31:17  
  140. http://dota.uuu9.com/201306/101412.shtml  
  141.   
  142.      
  143. [视频] DotA搞笑视频:论演员自我修养第五期  
  144. http://dota.uuu9.com/201306/101411.shtml  
  145. 您还没有安装flash播放器,请点击这里安装  
  146. 2013-6-17 11:05:58  
  147. http://dota.uuu9.com/201306/101411.shtml  
  148.   
  149.      
  150. [视频] 每周DOTA搞笑镜头:FunnyTOP10第78期  
  151. http://dota.uuu9.com/201306/101410.shtml  
  152. 您还没有安装flash播放器,请点击这里安装  
  153. 2013-6-17 10:57:31  
  154. http://dota.uuu9.com/201306/101410.shtml  
  155.   
  156.      
  157. [视频] 小乖第一视角:酣畅淋漓的影魔二连发  
  158. http://dota.uuu9.com/201306/101409.shtml  
  159. 您还没有安装flash播放器,请点击这里安装  
  160. 2013-6-17 9:42:07  
  161. http://dota.uuu9.com/201306/101409.shtml  
  162.   
  163.      
  164. [视频] Nada从顶单排10:中单巫医与中单流浪  
  165. http://dota.uuu9.com/201306/101408.shtml  
  166. 您还没有安装flash播放器,请点击这里安装  
  167. 2013-6-17 9:33:45  
  168. http://dota.uuu9.com/201306/101408.shtml  
  169.   
  170.      
  171. [视频] P神怒秀sf WGT胜者组Greedy vs PMM  
  172. http://dota.uuu9.com/201306/101402.shtml  
  173. WGT胜者组决赛Greedy vs PMM视频回顾  
  174. 2013-6-16 12:32:10  
  175. http://dota.uuu9.com/201306/101402.shtml  
  176.   
  177.      
  178. [视频] 满楼水平第一视角:跳刀沙王就是干  
  179. http://dota.uuu9.com/201306/101405.shtml  
  180. 您还没有安装flash播放器,请点击这里安装  
  181. 2013-6-16 12:28:48  
  182. http://dota.uuu9.com/201306/101405.shtml  
  183.   
  184.      
  185. [视频] ks从1800单排9-11:火女,女王,毒狗  
  186. http://dota.uuu9.com/201306/101404.shtml  
  187. 您还没有安装flash播放器,请点击这里安装  
  188. 2013-6-16 12:22:02  
  189. http://dota.uuu9.com/201306/101404.shtml  
  190.   
  191.      
  192. [视频] 直播回顾 浅浅打野斧王第一视角  
  193. http://dota.uuu9.com/201306/101401.shtml  
  194. 您还没有安装flash播放器,请点击这里安装  
  195. 2013-6-16 11:54:02  
  196. http://dota.uuu9.com/201306/101401.shtml  
  197.   
  198.      
  199. [视频] DOTA直播回顾:天使焦先知第一视角  
  200. http://dota.uuu9.com/201306/101400.shtml  
  201. 您还没有安装flash播放器,请点击这里安装  
  202. 2013-6-16 11:48:13  
  203. http://dota.uuu9.com/201306/101400.shtml  
  204.   
  205.      
  206. [图赏] WGT2013现场图赏 PIS再战职业首秀  
  207. http://dota.uuu9.com/201306/101393.shtml  
  208.   
  209. 2013-6-15 14:30:08  
  210. http://dota.uuu9.com/201306/101393.shtml  
  211.   
  212.      
  213. [视频] 小满鱼人第一视角:重回高手房匹配  
  214. http://dota.uuu9.com/201306/101392.shtml  
  215. 您还没有安装flash播放器,请点击这里安装  
  216. 2013-6-15 13:14:32  
  217. http://dota.uuu9.com/201306/101392.shtml  
  218.   
  219.      
  220. [视频] 舞儿蓝猫第一视角:劣势局飘逸逆袭  
  221. http://dota.uuu9.com/201306/101391.shtml  
  222. 您还没有安装flash播放器,请点击这里安装  
  223. 2013-6-15 13:09:42  
  224. http://dota.uuu9.com/201306/101391.shtml  
  225.   
  226.      
  227. [视频] 凯文教你打Carry:4V5的翻盘小黑  
  228. http://dota.uuu9.com/201306/101385.shtml  
  229. 您还没有安装flash播放器,请点击这里安装  
  230. 2013-6-15 0:43:04  
  231. http://dota.uuu9.com/201306/101385.shtml  
  232.   
  233.      
  234. [视频] Ks从1800单排7-8:老虎,先知无剧透  
  235. http://dota.uuu9.com/201306/101382.shtml  
  236. 您还没有安装flash播放器,请点击这里安装  
  237. 2013-6-14 16:51:55  
  238. http://dota.uuu9.com/201306/101382.shtml  
  239.   
  240.      
  241. [视频] 820dota第一视角:6.78新版暴力武士  
  242. http://dota.uuu9.com/201306/101377.shtml  
  243. 您还没有安装flash播放器,请点击这里安装  
  244. 2013-6-14 14:25:53  
  245. http://dota.uuu9.com/201306/101377.shtml  
  246.   
  247.      
  248. [视频] 09从零单排第二季41-42:山岭黑鸟  
  249. http://dota.uuu9.com/201306/101376.shtml  
  250. 您还没有安装flash播放器,请点击这里安装  
  251. 2013-6-14 14:13:57  
  252. http://dota.uuu9.com/201306/101376.shtml  
  253.   
  254.      
  255. [视频] Zero_C出品:极限反杀Top10 臂章特辑  
  256. http://dota.uuu9.com/201306/101372.shtml  
  257. 您还没有安装flash播放器,请点击这里安装  
  258. 2013-6-14 10:24:41  
  259. http://dota.uuu9.com/201306/101372.shtml  
  260.   
  261.      
  262. [视频] 牛蛙月骑第一视角:拉远古极限FARM  
  263. http://dota.uuu9.com/201306/101370.shtml  
  264. 您还没有安装flash播放器,请点击这里安装  
  265. 2013-6-14 9:30:59  
  266. http://dota.uuu9.com/201306/101370.shtml  
  267.   
  268.      
  269. [视频] Nada从顶单排9:撼地神牛第一视角  
  270. http://dota.uuu9.com/201306/101369.shtml  
  271. 您还没有安装flash播放器,请点击这里安装  
  272. 2013-6-14 9:22:33  
  273. http://dota.uuu9.com/201306/101369.shtml  
  274.   
  275.      
  276. [视频] Ks水友赛第一周:天差地别的两盘给跪  
  277. http://dota.uuu9.com/201306/101368.shtml  
  278. 您还没有安装flash播放器,请点击这里安装  
  279. 2013-6-14 9:15:24  
  280. http://dota.uuu9.com/201306/101368.shtml  
  281.   
  282.      
  283. [视频] 小乖顶端的开始7:激情影魔小鱼合集  
  284. http://dota.uuu9.com/201306/101364.shtml  
  285. 您还没有安装flash播放器,请点击这里安装  
  286. 2013-6-13 16:12:29  
  287. http://dota.uuu9.com/201306/101364.shtml  


这里我感觉到有点奇怪,这里解析出来的数据比服务器返回的数据要少。以前我在做Android解析XML的时候也是仅仅解析出来了一半,不过使用纯java的话,是可以全部解析出来的。这应该是从游久返回的数据有问题,我解析从自己搭建的服务器返回的数据是完美的。

 一般情况下我是要上传工程的源代码的,这次上传出了问题,过几天再上传吧。

0 0
原创粉丝点击