Cocos2dx利用CCSAXParser解析XML数据
1.、准备素材(xml文件)
testExcel.xml
<?xml version="1.0" encoding="utf-8"?><himiTestData><key1>1000</key1><key2>娃哈哈</key2><key3>82.3</key3><key4>4000</key4><key5>himi</key5><key6>true</key6></himiTestData>
2.实现代码:
解析XML类:HXmlParse类
HXmlParse.h
#ifndef __cocos2d_x_Excel__HXmlParse__
#define __cocos2d_x_Excel__HXmlParse__
#include <iostream>
#include "cocos2d.h"
#include "CCSAXParser.h"
#include "CCObject.h"
#include "CCDictionary.h"
using namespace std;
USING_NS_CC;
class HXmlParse:public CCObject,public CCSAXDelegator
{
public:
static HXmlParse * parserWithFile(const char *tmxFile);
bool initHXmlParse(const char* xmlName);
// 使用 CCSAXDelegator 重写3个回调函数
void startElement(void *ctx, const char *name, const char **atts);
void endElement(void *ctx, const char *name);
void textHandler(void *ctx, const char *ch, int len);
string root_name;
bool isJumpHeadData;
CCDictionary* mDic ;
private:
string startXmlElement;//用来记录每个key前字段
string endXmlElement;//用来记录每个key后字段
string currString;//记录每个value的值
};
#endif /* defined(__cocos2d_x_Excel__HXmlParse__) */
HXmlParse.cpp
#include "HXmlParse.h"
HXmlParse * HXmlParse::parserWithFile(const char *tmxFile)
{
HXmlParse *pRet = new HXmlParse();
if(pRet->initHXmlParse(tmxFile))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool HXmlParse::initHXmlParse(const char* xmlName)
{
mDic = new CCDictionary();
CCSAXParser _par;
if (false == _par.init("UTF-8") )
{
CCLog("-----请使用utf-8格式!");
return false;
}
_par.setDelegator(this);
const char* _path =CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(xmlName);
return _par.parse(_path);
}
//回调函数
void HXmlParse::startElement(void *ctx, const char *name, const char **atts)
{
CC_UNUSED_PARAM(ctx);
startXmlElement = (char*)name;
if(!isJumpHeadData){//跳过数据头
CCLog("------跳过root name");
isJumpHeadData=true;
root_name=startXmlElement;
return;
}
// CCLog("-startElement----%s",startXmlElement.c_str());
}
void HXmlParse::endElement(void *ctx, const char *name)
{
CC_UNUSED_PARAM(ctx);
endXmlElement = (char*)name;
if(endXmlElement==root_name){//数据尾
CCLog("读取xml结束");
isJumpHeadData=false;
root_name="";
return;
}
// CCLog("-endElement----%s",endXmlElement.c_str());
}
//键值对的结束字段
void HXmlParse::textHandler(void *ctx, const char *ch, int len)
{
CC_UNUSED_PARAM(ctx);
currString=string((char*)ch,0,len);
CCString *ccStr =new CCString();//备注3
ccStr->m_sString=currString;
if(root_name!=""){
mDic->setObject(ccStr,startXmlElement);
CCLog("-----key:%s, value:%s",startXmlElement.c_str(), ((CCString*)(mDic->objectForKey(startXmlElement)))->getCString());
}
// CCLog("-textHandler----%s",currString.c_str());
}
实现代码,在init内添加如下代码:
HXmlParse::parserWithFile("testExcel.xml");
结果如下:
Cocos2d: ------跳过root name
Cocos2d: -----key:key1, value:1000
Cocos2d: -----key:key2, value:娃哈哈
Cocos2d: -----key:key3, value:82.3
Cocos2d: -----key:key4, value:4000
Cocos2d: -----key:key5, value:feng
Cocos2d: -----key:key6, value:true
Cocos2d:读取xml结束
若想更深入了解有关解析XML,可参考下这篇博文:http://xiaominghimi.blog.51cto.com/2614927/841867