int MyTools::getIntValue(char* key)
{
int value = 0;
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
unsigned char* m_pBuffer = nullptr;
#if CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID
cocos2d::Data date = FileUtilsAndroid::getInstance()->getDataFromFile("GameData.xml");
m_pBuffer = date.getBytes();
#else
std::string m_sFilePath = FileUtils::getInstance()->fullPathForFilename("GameData.xml");
cocos2d::Data date = FileUtils::getInstance()->getDataFromFile(m_sFilePath);
m_pBuffer = date.getBytes();
#endif
doc->Parse((const char*)m_pBuffer);
auto rootNode = doc->FirstChildElement("GameDataRoot");
if (rootNode)
{
auto tempValue = rootNode->FirstChildElement(key)->FirstChild()->Value();
value = __String::create(tempValue)->intValue();
}
delete doc;
return value;
}
该方法为获取int类型的数据,因此传入一个Key返回一个整型数据。
考虑到跨平台所以加上预编译处理,不然在win32上运行成功的程序,打包到andriod会直接导致程序运行崩溃。