Cocos_2dx读取xml

##通过 key——value的形式读取XML

//XML格式





//代码 段

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;
}

你可能感兴趣的:(cocos,2dx,XML,Cocos2dx)