Skip to content

Instantly share code, notes, and snippets.

@kw0006667
Last active July 1, 2016 18:52
Show Gist options
  • Save kw0006667/5202516 to your computer and use it in GitHub Desktop.
Save kw0006667/5202516 to your computer and use it in GitHub Desktop.
Cocos2d-x Read/Write XML file
#include "CXmlStream.h"
CXmlStream::CXmlStream(const char* dicFilename)
{
this->m_dictionary = CCDictionary::createWithContentsOfFile(dicFilename);
}
CXmlStream::~CXmlStream()
{
}
const char* CXmlStream::GetStringByKey(const char* keyword)
{
return ((CCString*)(this->m_dictionary->objectForKey(keyword)))->m_sString.c_str();
}
const char* CXmlStream::GetStringByKeyFromFile(const char* dicFilename, const char* keyword)
{
CCDictionary *dictionary = CCDictionary::createWithContentsOfFile(dicFilename);
return ((CCString*)dictionary->objectForKey(keyword))->m_sString.c_str();
}
#ifndef __CXMLSTREAM_H__
#define __CXMLSTREAM_H__
#include "cocos2d.h"
USING_NS_CC;
class CXmlStream
{
private:
CCDictionary* m_dictionary;
public:
CXmlStream(const char* dicFilename);
~CXmlStream();
const char* GetStringByKey(const char* keyword);
static const char* GetStringByKeyFromFile(const char* dicFilename, const char* keyword);
};
#endif
<dict>
<key>HomeScene</key>
<string>首頁</string>
<key>MainScene</key>
<string>主頁</string>
<key>BookScene</key>
<string>百科</string>
<key>GameScene</key>
<string>遊戲</string>
<key>AnimScene</key>
<string>動畫</string>
<key>FOgameScene</key>
<string>力的作用</string>
<key>FOgameScene_Book</key>
<string>力的作用 - 百科</string>
<key>FOgameScene_Game</key>
<string>力的作用 - 遊戲</string>
<key>FOgameScene_Anim</key>
<string>力的作用 - 動畫</string>
<key>GVScene</key>
<string>地球引力</string>
<key>GVScene_Book</key>
<string>地球引力 - 百科</string>
<key>GVScene_Game</key>
<string>地球引力 - 遊戲</string>
<key>GVScene_Anim</key>
<string>地球引力 - 動畫</string>
</dict>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment