首页 技术 正文
技术 2022年11月15日
0 收藏 402 点赞 4,117 浏览 3123 个字

无意中起到一周中的游戏,哎,时间过得总是打得那么快时,。

。。

于是今天决定看一下之前不怎么非常熟悉的XML;(之前做游戏时数据的储存用到过XML。但这块是还有一个同事在做,所以不怎么熟悉),

看了看他写的xml和解析方法,然后自己改进了下,所以来简单的总结和分享分享

主要涉及到的有:

1. xml 创建

2.xml的解析

3.将解析后的xml数据用vector保存起来;

例如以下:(写完xml后,最简单的检查语法错误就是用IE浏览器打开看看,能够打开则说明语法没错)

<?xml version="1.0" encoding="utf-8"?>
<Mineral>
<mineral>
<type>1</type>
<times>100</times>
<p>20</p>
</mineral><mineral>
<type>4</type>
<times>100</times>
<p>20</p>
</mineral><mineral>
<type>5</type>
<times>100</times>
<p>20</p>
</mineral></Mineral>

在这里我依照网上的XML书写格式新建了一个名为 "Mineral.xml"的xml;

(Mineral就是矿的意思,xml 中我任意写了3中类型的矿石。每种矿石有自己的类型、倍率、概率)

然后将其保存在资源目录里面,然后新建一个cocos2d-x项目,

以下贴出主要解析代码

//.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"
#include <string>
#include <vector>typedef struct Mineral
{
int times ;
int type;
int p;}*PtrMineral;class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
bool readMinearlXml();
void displayVec();
CREATE_FUNC(HelloWorld);
private:
std::vector<PtrMineral >m_pMineralVec ;
};#endif

//.cpp文件

#include "HelloWorldScene.h"
#include "../support/tinyxml2/tinyxml2.h"using namespace tinyxml2;
USING_NS_CC;CCScene* HelloWorld::scene()
{ CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
readMinearlXml();
displayVec();
return true;
}bool HelloWorld::readMinearlXml()
{
tinyxml2::XMLDocument* xmlData = new tinyxml2::XMLDocument();
unsigned long nSize ;
const char *pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData("XML/Mineral.xml","rb",&nSize);if( NULL == pXmlBuffer )
{
CCLOG("read Mineral.xml Failed");
}
else
CCLOG("star read Mineral.xml");xmlData->Parse(pXmlBuffer,nSize);
XMLElement *rootNode = xmlData->RootElement();
if(!rootNode)
{
return false;
}
XMLElement* curNode = rootNode->FirstChildElement("mineral");while(NULL!= curNode)
{
PtrMineral pMineral =new Mineral();
pMineral->type = (atoi)( (curNode->FirstChildElement("type"))->GetText() );
pMineral->times = (atoi)( (curNode->FirstChildElement("times"))->GetText() );
pMineral->p = (atoi)( (curNode->FirstChildElement("p"))->GetText() );
m_pMineralVec.push_back(pMineral);
curNode = curNode->NextSiblingElement("mineral");
}
delete xmlData;
return true;}
void HelloWorld::displayVec()
{
CCLOG("*********m_pMineralVec*********");
for(int i = 0 ; i<m_pMineralVec.size() ; i++)
{
CCLOG("<mineral>");
CCLOG("<type> = %i </type>",m_pMineralVec[i]->type);
CCLOG("<times> = %i </times>",m_pMineralVec[i]->times);
CCLOG("<p> = %i </p>",m_pMineralVec[i]->p);
CCLOG("</mineral>");}}void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}

上面分别包括了xml的解析即xml的数据显示:

显演示样例如以下:

star read Mineral.xml
*********m_pMineralVec*********
<mineral>
<type> = 1 </type>
<times> = 100 </times>
<p> = 20 </p>
</mineral>
<mineral>
<type> = 4 </type>
<times> = 100 </times>
<p> = 20 </p>
</mineral>
<mineral>
<type> = 5 </type>
<times> = 100 </times>
<p> = 20 </p>
</mineral>

控制节目。输出和以前创建xml一致性,就这样,xml在分辨率ok 该,它不是很easy啊

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905