【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的话,是可以全部解析出来的。这应该是从游久返回的数据有问题,我解析从自己搭建的服务器返回的数据是完美的。
一般情况下我是要上传工程的源代码的,这次上传出了问题,过几天再上传吧。
【Cocos2d-x游戏引擎开发笔记(25)】XML解析的更多相关文章
- Python游戏引擎开发(五):Sprite精灵类和鼠标事件
		本次来实现Sprite类和鼠标事件. 说起这个Sprite啊,涉及过2D游戏研究领域的看官应该都听说过它. 它中文原意是"精灵",只是在不同人的眼中,它所表示的意义不同. 比方说在 ... 
- 推荐一些好用的 HTML5 & JavaScript 游戏引擎开发库
		推荐一些好用的 HTML5 & JavaScript 游戏引擎开发库 0. 引言 如果你是一个游戏开发者,并且正在寻找一个可以与 JavaScript 和 HTML5 无缝工作的游戏引擎.那么 ... 
- Python游戏引擎开发(七):绘制矢量图
		今天来完毕绘制矢量图形. 没有读过前几章的同学,请先阅读前几章: Python游戏引擎开发(一):序 Python游戏引擎开发(二):创建窗体以及重绘界面 Python游戏引擎开发(三):显示图片 P ... 
- 25 个超棒的 HTML5 & JavaScript 游戏引擎开发库
		就像在汽车中,引擎完成主要的工作,使汽车看起来不可思议.游戏引擎同理,游戏开发者完成细节的工作,使游戏看起来真实.吸引人眼球.游戏引擎负责其余的事情.早期,游戏开发者通常从草图做起,花费高昂,且不容易 ... 
- python开发笔记-通过xml快捷获取数据
		今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ... 
- 原生Js贪吃蛇游戏实战开发笔记
		前言 本课程是通过JavaScript结合WebAPI DOM实现的一版网页游戏---贪吃蛇的开发全过程,采用面向以象的思想设计开发.通过这个小游戏的开发, 不仅可以掌握JS的语法的应用,还可以学会D ... 
- XML学习笔记——关于XML解析器
		本篇文章基于W3C而写 在Firefox及其他浏览器中的XML解析器(除IE) var xmlDoc=document.implementation.createDocument("&quo ... 
- 《黑马程序猿》  cocos2d游戏引擎复习笔记一
		/** ----------------------------游戏场景的搭建-------------------------------- 1首先创建一个surfaceview ,它能够在子线程中 ... 
- 基于libgdx游戏引擎开发的飞天猫
		闲来没事学学游戏,这是鄙人第一个小游戏——飞天猫 1,基于Android开发的小游戏,至少Android2.2以上的系统. 2,界面简洁,美观,游戏易操作,上手快. 3,可以左右摇摆手机来改变飞天猫的 ... 
随机推荐
- 基于百度地图api + AngularJS 的入门地图
			转载请注明地址:http://www.cnblogs.com/enzozo/p/4368081.html 简介: 此入门地图为简易的“广州大学城”公交寻路地图,采用很少量的AngularJS进行inp ... 
- 条码知识之九:EAN-128条码(上)
			EAN-128码,现称GS1-128码,是专用于GS1系统中的条码,可以标注商品的附加信息,在商品信息的标识.产品的跟踪与追溯中有广泛的用途. EAN-128码来自于CODE-128码,在字符集.条 ... 
- android 修改背景色(转)
			修改为黑底白字 修改AndroidManifest.xml把android:theme="@style/AppTheme" 修改为android:theme="@andr ... 
- Android中如何判断是否联网
			首先在AndroidManifest.xml中添加与连接网络相关的权限: [xhtml] view plain copy <uses-permission android:name=&qu ... 
- redhat6.3安装matlab运行时MCR7.8,初步测试ok
			redhat6.3安装完matlab2008a后在目录$MATLAB_HOME/toolbox/compiler/deploy/glnxa64中有MCRInstaller.bin 使用这个安装MCR即 ... 
- Java:使用synchronized和Lock对象获取对象锁
			在并发环境下,解决共享资源冲突问题时,可以考虑使用锁机制. 1.对象的锁 所有对象都自动含有单一的锁. JVM负责跟踪对象被加锁的次数.如果一个对象被解锁,其计数变为0.在任务(线程)第一次给对象加锁 ... 
- c# datagridviewcomboboxcell值无效的解决办法
			一直认为是数据库存储的数据和datagridviewcomboboxcell对不上导致,今天碰到两者对应上了,预览的时候还是提示错误, 查看了下网上其他大神的解决方法,是数据库字段类型有误,查看了下, ... 
- css3属性——border-radius用法
			<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <style> ... 
- listview,fragment结合起来
			这是csdn上的以个demo,很适合初学者.来源:http://download.csdn.net/detail/endlife99/7274419,侵删. MainActiviy: package ... 
- bash有空格的文件名
			http://www.keakon.net/2011/10/20/bash%E4%B8%8B%E5%A4%84%E7%90%86%E5%8C%85%E5%90%AB%E7%A9%BA%E6%A0%BC ... 
