XML格式如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<MoneyTree>
<user uid="123456789" data="1381251737">
<swing times="1" award="20100" />
<swing times="2" award="21150" />
<swing times="3" award="13450" />
<swing times="4" award="19750" />
<swing times="5" award="18875" />
<swing times="6" award="9425" />
<swing times="7" award="9250" />
<swing times="8" award="17650" />
<swing times="9" award="21850" />
</user>
</MoneyTree>
现在需要在蓝色框中部分中显示摇钱树数据,数据保存在XML文件中,框中部分是一个TableView
1. 创建XML文件:
如果XML文件不存在,则创建一个:
// 创建XML文件,用于保存摇树数据
void CMoneyTreeView::CreateXMLFile()
{
string strFileFullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("MoneyTreeMsg.xml");
TiXmlDeclaration *xmlDecl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
TiXmlDocument *xmlDoc = new TiXmlDocument();
xmlDoc->LinkEndChild(xmlDecl);
TiXmlElement *xmlMTree = new TiXmlElement("MoneyTree");
xmlDoc->LinkEndChild(xmlMTree);
TiXmlElement *xmlUser = new TiXmlElement("user");
xmlUser->SetAttribute("uid", CPlayerDataMgr::instance()->GetPlayerInfoData().m_uid.c_str());
xmlUser->SetAttribute("data", time(NULL));
xmlMTree->LinkEndChild(xmlUser);
for (int i=0; i<m_config.timesPerDay; ++i)
{
TiXmlElement *xmlSwing = new TiXmlElement("swing");
if (i < m_shakeAward.size())
{
xmlSwing->SetAttribute("times", m_shakeAward[i].first);
xmlSwing->SetAttribute("award", m_shakeAward[i].second);
}
else
{
xmlSwing->SetAttribute("times", 0);
xmlSwing->SetAttribute("award", 0);
}
xmlUser->LinkEndChild(xmlSwing);
}
xmlDoc->SaveFile(strFileFullPath.c_str());
}
2. 读取XML文件
每次进入“摇钱树”页面时,都要读取一次XML文件,解析数据并初始化TableView
// 从XML文件中加载摇树数据
void CMoneyTreeView::ReadXMLFile()
{
string strFileFullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("MoneyTreeMsg.xml");
if (!CCFileUtils::sharedFileUtils()->isFileExist(strFileFullPath))
{ // XML文件不存在
m_shakeAward.clear();
return ;
}
TiXmlDocument *xmlDoc = new TiXmlDocument(strFileFullPath.c_str());
xmlDoc->LoadFile();
TiXmlElement *xmlMTree = xmlDoc->RootElement();
TiXmlElement *xmlUser = NULL;
for (xmlUser = xmlMTree->FirstChildElement("user");
NULL != xmlUser;
xmlUser = xmlUser->NextSiblingElement("user"))
{
string uid = CPlayerDataMgr::instance()->GetPlayerInfoData().m_uid;
if (0 == strcmp(uid.c_str(), xmlUser->Attribute("uid")))
{
m_shakeAward.clear();
// TODO:检查数据是否已经过期(不是当天就过期)
if (IsToday(atoi(xmlUser->Attribute("data"))))
{
TiXmlElement *xmlSwing = NULL;
for (xmlSwing = xmlUser->FirstChildElement("swing");
NULL != xmlSwing;
xmlSwing = xmlSwing->NextSiblingElement("swing"))
{
int times = atoi(xmlSwing->Attribute("times"));
int award = atoi(xmlSwing->Attribute("award"));
if (0 != times)
{
m_shakeAward.push_back(pair<int, int>(times, award));
}
}
xmlDoc->SaveFile(strFileFullPath.c_str());
return ;
}
else
{ // 不是当天,数据已经过期失效
return ;
}
}
}// end of for
}
3. 写XML文件
每次“摇树”以后,都需要更新XML文件中的数据
// 在XML文件中写入摇树信息
void CMoneyTreeView::WriteXMLFile()
{
string strFileFullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("MoneyTreeMsg.xml");
if (!CCFileUtils::sharedFileUtils()->isFileExist(strFileFullPath))
{ // XML文件不存在
CreateXMLFile();
return ;
}
TiXmlDocument *xmlDoc = new TiXmlDocument(strFileFullPath.c_str());
xmlDoc->LoadFile();
TiXmlElement *xmlMTree = xmlDoc->RootElement();
TiXmlElement *xmlUser = NULL;
for (xmlUser = xmlMTree->FirstChildElement("user");
NULL != xmlUser;
xmlUser = xmlUser->NextSiblingElement("user"))
{
string uid = CPlayerDataMgr::instance()->GetPlayerInfoData().m_uid;
if (0 == strcmp(uid.c_str(), xmlUser->Attribute("uid")))
{
// 更新一下摇树时间
xmlUser->SetAttribute("data", time(NULL));
TiXmlElement *xmlSwing = xmlUser->FirstChildElement("swing");
for (int i=0; i<m_shakeAward.size(); ++i)
{
xmlSwing->SetAttribute("times", m_shakeAward[i].first);
xmlSwing->SetAttribute("award", m_shakeAward[i].second);
xmlSwing = xmlSwing->NextSiblingElement("swing");
}
xmlDoc->SaveFile(strFileFullPath.c_str());
return ;
}
}
// 到这里,说明是另一个账号登陆,则添加一个账号信息
AddUserToXmlFile();
}