【Cocos2d-x游戏引擎开发笔记(25)】XML解析

来源:互联网 发布:cf手游刷枪软件ios 编辑:程序博客网 时间:2024/05/21 09:19
XML是一种非常重要的文件格式,由于C++对XML的支持非常完善,Cocos2d-x选择XML作为主要的文件存储格式。在Cocos2d-x中集成了libxml2来解析XML数据。

 

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

 

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


源文件

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


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

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

1
staticstring str;


在源文件中:

1
std::string HelloWorld::str;


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

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


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

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


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

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


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

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