欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
}
相关标签: XML Cocos2dx