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

cocos2d-x 使用TinyXML2存储数据:

 
阅读更多

最近换了cocos2d-x劳动节版,原来版本的CCUserDefault使用的是libxml2,劳动节版的使用了TinyXML2(貌似2.1.2就换了),有国外开发者说,他升级引擎后什么都没干,游戏在安卓上的加载时间从20秒缩短到了5秒。

因为CCUserDefault没有提供对节点属性的操作,所以我就简单写了一下针对节点内容以及节点属性的操作,简单封装,与大家分享下,基本该有的操作应该是全了【为了让读者方便使用,有两个方法是介绍使用的,你如果用本库,可以删除:createXMLFile();readXML()】,废话不多说,提供源码【目前只是简单使用,后期非逐渐完善,添加各种方法,对数据进行加密,就像《CCUserDefault加密》一文中的,可能表现为:《工具库:UtilTools》一文中的方式】……

FDTinyXML2.h

//
// FDTinyXML2.h
// RunnerEditor
//
// Created by firedragonpzy on 13-5-3.
//
//
#ifndef __RunnerEditor__FDTinyXML2__
#define __RunnerEditor__FDTinyXML2__
#include "support/tinyxml2/tinyxml2.h"
#include "cocos2d.h"
#include "platform/CCFileUtils.h"
#include "TempObject.h"
#include "RConfig.h"
#include <fstream>
USING_NS_CC;
using namespace tinyxml2;
using namespace std;
class FDTinyXML2 : public CCObject{
public:
bool init();
CREATE_FUNC(FDTinyXML2);
bool createXMLFile();
void readXML();
bool createXMLFileFromArray(CCArray *array,CCArray *array1);
bool readXMLToArray(CCArray* &array,cocos2d::CCArray *&array1);
private:
string m_sFilePath;
string m_sBackUpFilePath;
bool isXMLFileExist();
bool backupXMLFile();
bool copyFile(const char*pszFile, const char* newFile);
void groundNodeToArray(XMLElement *ground,CCArray *&array);
void eleNodeToArray(XMLElement *ele,CCArray *&array1);
};
#endif /* defined(__RunnerEditor__FDTinyXML2__) */





FDTinyXML2.cpp

//
// FDTinyXML2.cpp
// RunnerEditor
//
// Created by firedragonpzy on 13-5-3.
//
//
#include "FDTinyXML2.h"
// root name of xml
#define FDTinyXML2_ROOT_NAME "firedragonpzy"
#define XML_FILE_NAME "firedragonpzy.xml"
#define XML_BACKUP_FILE_NAME "firedragonpzy.bak"
#define XML_FIRST_NODE "grounds"
#define XML_SECOND_NODE "barriers"
bool FDTinyXML2::init()
{
if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)//if you don't
{
m_sFilePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(XML_FILE_NAME);
m_sBackUpFilePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(XML_BACKUP_FILE_NAME);
}else
{
m_sFilePath = CCFileUtils::sharedFileUtils()->getWritablePath() + XML_FILE_NAME;
m_sBackUpFilePath = CCFileUtils::sharedFileUtils()->getWritablePath() + XML_BACKUP_FILE_NAME;
}
return true;
}
// This is a demo, if you use my lib,you can delete this method createXMLFile()
bool FDTinyXML2::createXMLFile()
{
bool bRet = false;
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
if (NULL == pDoc) {
return false;
}
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
if (NULL == pDeclaration) {
return false;
}
pDoc->LinkEndChild(pDeclaration);
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(FDTinyXML2_ROOT_NAME);
if (NULL == pRootEle) {
return false;
}
pDoc->LinkEndChild(pRootEle);
tinyxml2::XMLElement *subGroundGrass = pDoc->NewElement("groundGrass");
tinyxml2::XMLText *content = pDoc->NewText("content:groudText");
subGroundGrass->LinkEndChild(content);
pRootEle->LinkEndChild(subGroundGrass);
tinyxml2::XMLElement *subGroundSoil = pDoc->NewElement("groundSoil");
subGroundSoil->SetAttribute("soil-attribute", "text:soil");
pRootEle->LinkEndChild(subGroundSoil);
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(m_sFilePath.c_str());
if (pDoc) {
delete pDoc;
}
return bRet;
}
// This is a demo, if you use my lib,you can delete this method readXML()
void FDTinyXML2::readXML()
{
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
tinyxml2::XMLElement *rootNode = NULL;
tinyxml2::XMLElement *curNode = NULL;
const tinyxml2::XMLAttribute *curAttribute = NULL;
do {
unsigned long nSize;
const char *pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData(m_sFilePath.c_str(), "rb", &nSize);
if (NULL == pXmlBuffer) {
break;
}
pDoc->Parse(pXmlBuffer);
rootNode = pDoc->RootElement();
if (NULL == rootNode) {
break;
}
curNode = rootNode->FirstChildElement();
XMLElement *secondNode = curNode->NextSiblingElement();
CCLog("---------------Test------------------");
CCLog("GetText():%s",secondNode->GetText());
CCLog("Name():%s",secondNode->Name());
CCLog("Value():%s",secondNode->Value());
CCLog("---------------Test------------------");
curAttribute = curNode->FirstAttribute();
while (NULL != curNode) {
CCLog("GetText():%s",curNode->GetText());
CCLog("Value():%s",curNode->Value());
while (NULL != curAttribute) {
CCLog("curAttribute->Name():%s",curAttribute->Name());
CCLog("curAttribute->Value():%s",curAttribute->Value());
curAttribute = curAttribute->Next();
}
curNode = curNode->NextSiblingElement();
if (curNode) {
curAttribute = curNode->FirstAttribute();
}
CCLog("---------------END----------------");
}
if (pDoc) {
delete pDoc;
}
} while (0);
}
// Determine if xml exists.
bool FDTinyXML2::isXMLFileExist()
{
CCLog("%s",m_sFilePath.c_str());
FILE *fp = fopen(m_sFilePath.c_str(), "r");
bool bRet = false;
if (fp) {
bRet = true;
fclose(fp);
}
return bRet;
}
bool FDTinyXML2::backupXMLFile()
{
bool bRet = false;
if (isXMLFileExist()) {
if (copyFile(m_sFilePath.c_str(), m_sBackUpFilePath.c_str())) {
bRet = true;
}else
{
bRet = false;
}
}else
{
bRet = false;
CCLog("no file exists!");
}
return bRet;
}
bool FDTinyXML2::copyFile(const char *pszFile, const char *newFile)
{
ifstream input;
ofstream output;
input.open(pszFile,ios::binary);
if (input.fail()) {
input.close();
output.close();
return false;
}
output.open(newFile,ios::binary);
if (output.fail()) {
input.close();
output.close();
return false;
}else
{
output<<input.rdbuf();
output.close();
input.close();
return true;
}
}
bool FDTinyXML2::createXMLFileFromArray(cocos2d::CCArray *array,CCArray *array1)
{
bool bRet = false;
if (isXMLFileExist()) {
if (remove(m_sFilePath.c_str()))
{
return false;
}
}
XMLDocument *pDoc = new XMLDocument();
if (NULL == pDoc) {
return false;
}
XMLDeclaration *pDeclaration = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
if (NULL == pDeclaration) {
return false;
}
pDoc->LinkEndChild(pDeclaration);
XMLElement *pRootEle = pDoc->NewElement(FDTinyXML2_ROOT_NAME);
if (NULL == pRootEle) {
return false;
}
pDoc->LinkEndChild(pRootEle);
TempObject *tempObj = NULL;
XMLElement *subElement = NULL;
if (array ) {
int count = array->count();
if (count > 0) {
XMLElement *firstNode = pDoc->NewElement(XML_FIRST_NODE);
if (NULL != firstNode) {
pRootEle->LinkEndChild(firstNode);
}else
{
return false;
}
for (int i = 0; i < count; i++) {
tempObj = (TempObject*)array->objectAtIndex(i);
GroundBarrierStruct groundStruct = tempObj->getGroundBarrier();
subElement = pDoc->NewElement(groundStruct.bType.c_str());
subElement->SetAttribute(XML_POSX, groundStruct.posx);
subElement->SetAttribute(XML_POSY, groundStruct.posy);
subElement->SetAttribute(XML_FLAG, groundStruct.flag);
firstNode->LinkEndChild(subElement);
}
}
}
if (array1 ) {
int count = array1->count();
if (count > 0) {
XMLElement *secondNode = pDoc->NewElement(XML_SECOND_NODE);
if (NULL != secondNode) {
pRootEle->LinkEndChild(secondNode);
}else
{
return false;
}
for (int i = 0; i < count; i++) {
tempObj = (TempObject*)array1->objectAtIndex(i);
EleBarrierStuct eleStruct = tempObj->getEleBarrier();
subElement = pDoc->NewElement(eleStruct.bType.c_str());
subElement->SetAttribute(XML_POSX, eleStruct.posx);
subElement->SetAttribute(XML_POSY, eleStruct.posy);
subElement->SetAttribute(XML_FLAG, eleStruct.flag);
secondNode->LinkEndChild(subElement);
}
}
}
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(m_sFilePath.c_str());
if (pDoc) {
delete pDoc;
}
// this method usets to back up the file,if you don't need,you can delete.
backupXMLFile();
return bRet;
}
bool FDTinyXML2::readXMLToArray(cocos2d::CCArray *&array,cocos2d::CCArray *&array1)
{
bool bRet = false;
if (isXMLFileExist()) {
do {
XMLDocument *xmlDoc = new XMLDocument();
// const XMLAttribute *curAttribute = NULL;
unsigned long nSize;
const char* xmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData(m_sFilePath.c_str(), "rb", &nSize);
if (NULL == xmlBuffer) {
return false;
}
xmlDoc->Parse(xmlBuffer);
delete [] xmlBuffer;
XMLElement *rootNode = xmlDoc->RootElement();
if (NULL == rootNode) {
return false;
}
// first element
XMLElement *firstNode = rootNode->FirstChildElement();
if (NULL == firstNode) {
return false;
}
XMLElement *subFirstNode = firstNode->FirstChildElement();
if (NULL == subFirstNode) {
return false;
}
if (strncmp(firstNode->Name(),XML_FIRST_NODE,strlen(XML_FIRST_NODE)) == 0) {
groundNodeToArray(subFirstNode,array);
}else if (strncmp(firstNode->Name(),XML_SECOND_NODE,strlen(XML_SECOND_NODE)) == 0)
{
eleNodeToArray(subFirstNode, array1);
}
// next element
XMLElement *secondNode = firstNode->NextSiblingElement();
if (NULL == secondNode) {
return true;
}
XMLElement *subSecondNode = secondNode->FirstChildElement();
if (NULL == subSecondNode) {
return true;
}
if (strncmp(secondNode->Name(),XML_FIRST_NODE,strlen(XML_FIRST_NODE)) == 0) {
groundNodeToArray(subSecondNode,array);
}else if (strncmp(secondNode->Name(),XML_SECOND_NODE,strlen(XML_SECOND_NODE)) == 0)
{
eleNodeToArray(subSecondNode, array1);
}
} while (0);
bRet = true;
}else
{
bRet = false;
}
return bRet;
}
void FDTinyXML2::groundNodeToArray(XMLElement *ground,CCArray *&array)
{
const XMLAttribute *curAttribute = NULL;
TempObject *obj = NULL;
GroundBarrierStruct groundStruct;
while (NULL != ground) {
curAttribute = ground->FirstAttribute();
if (strncmp(curAttribute->Name(), XML_POSX,strlen(XML_POSX)) == 0) {
groundStruct.posx = atof(curAttribute->Value());
}
groundStruct.bType = ground->Name();
obj = TempObject::create();
while (NULL != curAttribute) {
if (strncmp(curAttribute->Name(), XML_POSX,strlen(XML_POSX)) == 0) {
groundStruct.posx = atof(curAttribute->Value());
}else if (strncmp(curAttribute->Name(), XML_POSY,strlen(XML_POSY)) == 0)
{
groundStruct.posy = atof(curAttribute->Value());
}else if (strncmp(curAttribute->Name(), XML_FLAG,strlen(XML_FLAG)) == 0)
{
groundStruct.flag = atoi(curAttribute->Value());
}
obj->setGroundBarrier(groundStruct);
curAttribute = curAttribute->Next();
}
array->addObject(obj);
ground = ground->NextSiblingElement();
}
}
void FDTinyXML2::eleNodeToArray(tinyxml2::XMLElement *ele, cocos2d::CCArray *&array1)
{
EleBarrierStuct eleStruct;
TempObject *obj = NULL;
const XMLAttribute *curAttribute = NULL;
while (NULL != ele) {
curAttribute = ele->FirstAttribute();
eleStruct.bType = ele->Name();
obj = TempObject::create();
while (NULL != curAttribute) {
if (strncmp(curAttribute->Name(), XML_POSX,strlen(XML_POSX)) == 0) {
eleStruct.posx = atof(curAttribute->Value());
}else if (strncmp(curAttribute->Name(), XML_POSY, strlen(XML_POSY)) == 0)
{
eleStruct.posy = atof(curAttribute->Value());
}else if (strncmp(curAttribute->Name(), XML_FLAG, strlen(XML_FLAG)) == 0)
{
eleStruct.flag = atoi(curAttribute->Value());
}
obj->setEleBarrier(eleStruct);
curAttribute = curAttribute->Next();
}
array1->addObject(obj);
ele = ele->NextSiblingElement();
}
}



暂且提供这些,后续补充,忘大家多多提供宝贝意见……

注意事项:

在win中开发的时候:

1、使用XMLDocument的时候要添加tinyxml2命名空间,否则error:不明确的符号

2、目前来说tinyxml2还不能直接引用处理,需要自己include

2013、05、09【v1.0】

2013、05、10【v1.1】更新:

1、修改了读取的bug

2、调整了win上面文件存储的位置,v1.0存储在可读路径下,如果打包exe,无法获取xml文件。现在存储在Resouces路径下……

Demo下载链接:https://github.com/firedragonpzy/FD_Libs.git


分享到:
评论

相关推荐

    Cocos2d-x实战:JS卷——Cocos2d-JS开发

    资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...

    cocos2d-x-2.1.5

    cocos2d-x-2.1.5

    cocos2d-x事件类

    在使用cocos2d-x开发游戏的过程中,为了实现逻辑和显示相分离。 在下通宵了一个晚上,写出了该事件类。 谨记,该事件只能用于cocos2d-x中。 事件发送者需要继承EventDispatcher类 事件接收者需要继承EventHandle类...

    大富翁手机游戏开发实战基于Cocos2d-x3.2引擎

    资源名称:大富翁手机游戏开发实战基于Cocos2d-x3.2引擎内容简介:李德国编著的《大富翁手机游戏开发实战(基于 Cocos2d-x3.2引擎)》使用Cocos2d-x游戏引擎技术,带领读者一步一步从零开始进行大富翁移动游戏的开发...

    Cocos2d-x高级开发教程

    Cocos2d-x是移动跨平台开发最流行的游戏引擎,而本书是一本很全面的、比较‘接地气’的游戏开发教程。书中汇聚了热门手机游戏《捕鱼达人》开发的实战经验,作者从最基础的内容开始,逐步深入地介绍了Cocos2d-x的相关...

    cocos2d-x-3.2旧版引擎下载

    cocos2d-x-3.2下载,不多说。或者可以下载另一个资源 cocos引擎老版本集合(cocos2d-x-2.2.1 - 3.5) http://download.csdn.net/download/crazymagicdc/9982656

    cocos2d-x实战项目

    cocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML文件读取与骨骼动画.rarcocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML...

    Cocos2D-X游戏开发技术精解

     《Cocos2D-X游戏开发技术精解》详细介绍如何使用Cocos2D-X引擎开发自己的移动平台游戏。全书共15章,主要内容包括:Cocos2D-X引擎简介;如 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    cocos2d-x-3.0 类图

    这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...

    精通COCOS2D-X游戏开发 基础卷_2016.4-P399-13961841.pdf

    精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发

    Cocos2D-X游戏开发技术精解.pdf

    《Cocos2D-X游戏开发技术精解》详细介绍如何使用Cocos2D-X引擎开发自己的移动平台游戏。全书共15章,主要内容包括:Cocos2D-X引擎简介;如何建立跨平台的开发环境;引擎的核心模块——渲染框架;如何实现动态画面和...

    Cocos2d-x实战 JS卷

    Cocos2d-x实战

    Cocos2d-x实战 JS卷 Cocos2d-JS开发

    Cocos2d-x实战 JS卷 Cocos2d-JS开发 PDF 电子书完整版本

    Cocos2d-x 3.x游戏开发实战pdf含目录

    Cocos2d-x 3.x游戏开发实战pdf含目录,内容详细,强烈推荐给大家。

    Cocos2d-x高级开发教程制作自己的《捕鱼达人》

    Cocos2d-x高级开发教程:制作自己的《捕鱼达人》 图书简介: 《Cocos2d-x高级开发教程:制作自己的《捕鱼达人》》是国内第一本全面深入讲解Cocos2d-x进阶内容的图书,Cocos2d-x创始人王哲作序推荐,《捕鱼达人》开发...

    cocos2d-x 3.0

    cocos2d-x 3.0 人物行走 . 包里有代码和 图片资源.

    cocos2d-x windows vs2010配置

    Cocos2d-x windows vs2010 配置图文详解

    cocos2d-x开发者文档(中文)2015-01-30

    因为最近在学cocos2d-x,找了半天在网上也找不到一个离线的文档,于是自己抽空做了一个,全部内容提取自cocos2d-x中文官网的文档页http://cn.cocos2d-x.org/article 目前只提取了cocos2d-x部分内容。因为内容比较多...

Global site tag (gtag.js) - Google Analytics