Cocos_2dx读取xml
程序员文章站
2022-06-14 13:40:25
...
##通过 key——value的形式读取XML
//XML格式
<?xml version="1.0" encoding="utf8"?>
<root>
<item key="Client_Version" text="1.0" />
<root>
//代码 段
bool VLocalize::Init()
{
cocos2d::Data data = cocos2d::FileUtils::getInstance()->getDataFromFile("Localize/Localize.xml");
if (data.isNull())
{
return false;
}
tinyxml2::XMLDocument xml;
tinyxml2::XMLError Error = xml.Parse((const char*)data.getBytes(), data.getSize());
if (Error != tinyxml2::XML_SUCCESS)
{
assert(0);
return false;
}
tinyxml2::XMLElement* Root = xml.RootElement();
tinyxml2::XMLElement* Child = Root->FirstChildElement();
std::string Key;
std::string Text;
while (Child)
{
Key = Child->Attribute("key");
Text = Child->Attribute("text");
mItems[Key] = Text;
Child = Child->NextSiblingElement();
}
return true;
}
const std::string& VLocalize::GetText(const std::string& Key) const
{
auto itr = mItems.find(Key);
if ( itr != mItems.end())
{
return itr->second;
}
return NullString;
}
上一篇: Excel表格数据导入mysql数据库中
下一篇: 九宫格