一不小心就玩了一周的游戏了。哎。玩的时候时间过得总是这么快。。。
于是今天决定看一下之前不怎么非常熟悉的 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 啊
来源: http://www.bubuko.com/infodetail-2094856.html