1. 程式人生 > >cocos2d-x 中XML解析與數據存儲

cocos2d-x 中XML解析與數據存儲

lba false 網上 unsigned failed popu new ccm cfile

一不小心就玩了一周的遊戲了。哎。玩的時候時間過得總是這麽快。。。

技術分享

於是今天決定看一下之前不怎麽非常熟悉的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啊技術分享

cocos2d-x 中XML解析與數據存儲