Cocos2d-x自定义XML文件的读取

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会直接导致程序运行崩溃。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值