`
modabobo
  • 浏览: 505836 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

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

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

#ifndef XMLANALYSIS_H_H
#define XMLANALYSIS_H_H
#include "cocos2d.h"
#include <string>
using namespace cocos2d;
using namespace std;
class XMLAnalysis:public CCSAXDelegator,public CCObject//继承CCSAXDelegator,覆盖纯虚函数
{
public:
	XMLAnalysis(const char* data,unsigned int length);//解析数据
	XMLAnalysis(const char* filename);					  //解析文件
	~XMLAnalysis(void);
	virtual void startElement(void *ctx, const char *name, const char **atts);//开始标签
    virtual void endElement(void *ctx, const char *name);                     //标签结束
    virtual void textHandler(void *ctx, const char *s, int len);              //标签的内容
	string rootname;
};
#endif


源文件

#include "XMLAnalysis.h"


XMLAnalysis::XMLAnalysis(const char* 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(const char* filename)
{
	CCSAXParser parser;
	if(parser.init("UTF-8")==0)
	{
		CCLog("please use utf-8");
		return;
	}
	parser.setDelegator(this);
	parser.parse(filename);
}


XMLAnalysis::~XMLAnalysis(void)
{
	
}
void XMLAnalysis::startElement(void *ctx, const char *name, const char **atts)
{
	
	if(strcmp(name,"root"))
	{
	rootname=name;
	}
}
void XMLAnalysis::textHandler(void *ctx, const char *s, int len)
{
	string str=string(s,0,len);
	
	if(rootname!="")
	{
		CCLog("%s",str.c_str());	
	}
}

void XMLAnalysis::endElement(void *ctx, const char *name)
{
	
	rootname="";
}


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

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

static string str;


在源文件中:

std::string HelloWorld::str;


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

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	str="";
	//usePost();
	useGet();
	XMLAnalysis *p=new XMLAnalysis(str.c_str(),strlen(str.c_str()));//解析数据
	//XMLAnalysis *p=new XMLAnalysis("xmltemp.xml");				//解析文件
	delete p;
}


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

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中

size_t HelloWorld::writehtml(uint8_t* ptr,size_t size,size_t number,void *stream)
{
	char* buffer=new char[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;
	return size*number;//这里一定要放回实际返回的字节数
}


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

  
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-14 16:51:55
http://dota.uuu9.com/201306/101382.shtml

   
[视频] 820dota第一视角:6.78新版暴力武士
http://dota.uuu9.com/201306/101377.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-14 14:25:53
http://dota.uuu9.com/201306/101377.shtml

   
[视频] 09从零单排第二季41-42:山岭黑鸟
http://dota.uuu9.com/201306/101376.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-14 14:13:57
http://dota.uuu9.com/201306/101376.shtml

   
[视频] Zero_C出品:极限反杀Top10 臂章特辑
http://dota.uuu9.com/201306/101372.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-14 10:24:41
http://dota.uuu9.com/201306/101372.shtml

   
[视频] 牛蛙月骑第一视角:拉远古极限FARM
http://dota.uuu9.com/201306/101370.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-14 9:30:59
http://dota.uuu9.com/201306/101370.shtml

   
[视频] Nada从顶单排9:撼地神牛第一视角
http://dota.uuu9.com/201306/101369.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-14 9:22:33
http://dota.uuu9.com/201306/101369.shtml

   
[视频] Ks水友赛第一周:天差地别的两盘给跪
http://dota.uuu9.com/201306/101368.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-14 9:15:24
http://dota.uuu9.com/201306/101368.shtml

   
[视频] 小乖顶端的开始7:激情影魔小鱼合集
http://dota.uuu9.com/201306/101364.shtml
您还没有安装flash播放器,请点击这里安装
2013-6-13 16:12:29
http://dota.uuu9.com/201306/101364.shtml


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

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics