首先说明,在Android平台下
FileUtils::getInstance()
这玩意不能在静态方法或静态类(全局范围内)中调用,具体原因网上有,这里就不多做解释了。
第二,以下写法是错误例子:
#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2;
...
std::string path = FileUtils::getInstance()->fullPathForFilename("res/data.xml");
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
XMLError error = doc->LoadFile(path.c_str());
if (error == 0) {
// do something...
}
原因是LoadFile中会调用fopen,在win32平台下没有问题,但android下一般情况下无法使用fopen。
以下是正确写法(不唯一)
#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2;
...
std::string path = FileUtils::getInstance()->getStringFromFile("res/data.xml");
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
XMLError error = doc->Parse(path.c_str(), path.size());
if (error == 0) {
// do something...
}