如题
获取int型数值的静态函数:
int Tools::getIntData(const char* dataName, const char* filepath)
{
int data=0;
//CCLOG("Loading File.");
tinyxml2::XMLDocument* pDoc = new tinyxml2::XMLDocument();
ssize_t bufferSize;
auto pBuffer = CCFileUtils::sharedFileUtils()->getFileData(filepath, "rb", &bufferSize);
pBuffer[(int)bufferSize - 1] = '\0';
int errorId = pDoc->Parse((const char*)pBuffer);
//若不存在则报错
if (errorId != 0)
{
//解析xml文件错误
CCLOG("xml anlyatic error: %d", errorId);
return 0;
}
tinyxml2::XMLElement *rootElement = pDoc->RootElement();
//CCLOG("<%s version=\"1.0\">", rootElement->Value());
// 获取第一个子节点
tinyxml2::XMLElement *pElement = rootElement->FirstChildElement();
while (pElement != NULL)
{
std::string str;
str += " ";
str += "<";
str += pElement->Value();
str += " ";
auto attr = pElement->FirstAttribute();
while (attr != NULL)
{
str += attr->Name();//获取名字
str += "=\"";
str += attr->Value();//获取值
str += "\" ";
if (strcmp(pElement->Name(), dataName) == 0)
{
data= std::atoi(attr->Value());
CCLOG("IntValue:%d", std::atoi(attr->Value()));
break;
}
attr = attr->Next();
}
str += "/>";
//CCLOG("%s", str.c_str());//输出所有的信息
// 进入下一个节点
pElement = pElement->NextSiblingElement();
}
//CCLOG("</%s>", rootElement->Value());
//pDoc->DeleteChildren();
delete pDoc;
return data;
}
设置int型数值的函数:
bool Tools::setIntData(const char* dataName, int data, const char* filepath)
{
//CCLOG("Loading File.");
//文件目录
tinyxml2::XMLDocument* pDoc = new tinyxml2::XMLDocument();
ssize_t bufferSize;
auto pBuffer = CCFileUtils::sharedFileUtils()->getFileData(filepath, "rb", &bufferSize);
pBuffer[(int)bufferSize - 1] = '\0';
int errorId = pDoc->Parse((const char*)pBuffer);
//若不存在则报错
if (errorId != 0)
{
//解析xml文件错误
CCLOG("xml anlyatic error: %d", errorId);
return false;
}
tinyxml2::XMLElement *rootElement = pDoc->RootElement();
// 获取第一个子节点
tinyxml2::XMLElement *pElement = rootElement->FirstChildElement();
while (pElement != NULL)
{
auto attr = pElement->FirstAttribute();
if (strcmp(pElement->Name(), dataName) == 0)
{
pElement->SetAttribute("data", data);
CCLOG("Set Int Data Success %d",std::atoi(pElement->FirstAttribute()->Value()));
}
// 进入下一个节点
pElement = pElement->NextSiblingElement();
}
CCLOG("%d",pDoc->SaveFile(filepath));
pDoc->Print();
pDoc->DeleteChildren();
delete pDoc;
return true;
}
获取和设置属性都没有问题,只是在修改xml后再读取该xml文件获取的值不变,用Parse就数就会有这样的问题,LoadFile就没事,可是用LoadFile在安卓机上又不能用,这是怎么回事?是内存没有释放?还是和Parse函数相关,怎么解决呢?