void XMLParse:: ParseScene()
{
scenesArray=CCArray::create();
scenesArray->retain();
std::string xmlPath=std::string("scene.xml");
FengZhuang::FileCopy(xmlPath.c_str()); //需要调用拷贝文件到目录的操作
std::string filePath=CCFileUtils::sharedFileUtils()->getWritablePath();
std::string filepath=filePath+xmlPath;
TiXmlDocument * document=new TiXmlDocument(filepath.c_str());
document->LoadFile();
TiXmlElement * rootElement=document->RootElement();
CCLOG("rootKey:%s",rootElement->Value());
CCLOG("Scenes=%s",rootElement->GetText());
TiXmlElement * ScenesElement=rootElement->FirstChildElement();
CCLOG("ScenesKey:%s",ScenesElement->Value());
CCLOG("ScenesElement=%s",ScenesElement->GetText());
TiXmlElement * sceneElement=ScenesElement->FirstChildElement();
while (sceneElement) {
TiXmlElement * sceneNameElement=sceneElement->FirstChildElement();
const char * name=sceneNameElement->GetText();
TiXmlElement * sceneNumberElement=sceneNameElement->NextSiblingElement();
int scenenumber=atoi(sceneNumberElement->GetText());
TiXmlElement * chapterNumElement=sceneNumberElement->NextSiblingElement();
int chapterNum=atoi(chapterNumElement->GetText());
TiXmlElement * unLockElement=chapterNumElement->NextSiblingElement();
int unLockNum=atoi(unLockElement->GetText());
TiXmlElement * needstarNumElement=unLockElement->NextSiblingElement();
int needstarNum=atoi(needstarNumElement->GetText());
SceneData * scenedata=new SceneData(name,scenenumber,chapterNum,unLockNum,needstarNum);
scenesArray->addObject(scenedata);
sceneElement=sceneElement->NextSiblingElement();
}
//delete document;
}
void XMLParse:: ParseChapter(int scenenumber)
{
chaptersArray=CCArray::create();
chaptersArray->retain();
char xmlname[20];
sprintf(xmlname, "Chapter%d.xml",scenenumber);
FengZhuang::FileCopy(xmlname);
std::string xmlPath=std::string(xmlname);
std::string filePath=CCFileUtils::sharedFileUtils()->getWritablePath();
std::string filepath=filePath+xmlPath;
TiXmlDocument * document=new TiXmlDocument(filepath.c_str());
document->LoadFile();
TiXmlElement * rootElement=document->RootElement();
CCLOG("rootElement:%s",rootElement->Value());
CCLOG("rootElement=%s",rootElement->GetText());
TiXmlElement * LevelsElement=rootElement->FirstChildElement();
CCLOG("LevelsElement:%s",LevelsElement->Value());
CCLOG("LevelsElement=%s",LevelsElement->GetText());
TiXmlElement * levelElement=LevelsElement->FirstChildElement();
while (levelElement) {
TiXmlElement * levelNameElement=levelElement->FirstChildElement();
const char * name=levelNameElement->GetText();
TiXmlElement * levelNumberElement=levelNameElement->NextSiblingElement();
int levelnumber=atoi(levelNumberElement->GetText());
TiXmlElement * unLockElement=levelNumberElement->NextSiblingElement();
int unlock=atoi(unLockElement->GetText());
TiXmlElement * starNumElement=unLockElement->NextSiblingElement();
int starnumber=atoi(starNumElement->GetText());
TiXmlElement * highestscoreElement=starNumElement->NextSiblingElement();
int highestscore=atoi(highestscoreElement->GetText()) ;
ChapterData * chapterdata=new ChapterData(name,levelnumber,unlock,0,highestscore,starnumber);
chaptersArray->addObject(chapterdata);
levelElement=levelElement->NextSiblingElement();
}
//delete document;
}
//更新xml的数据操作
void XMLParse:: updateScenexml(int getNumstar,int sceneNum)
{
std::string xmlPath=std::string("scene.xml");
FengZhuang::FileCopy(xmlPath.c_str());
std::string filePath=CCFileUtils::sharedFileUtils()->getWritablePath();
std::string filepath=filePath+xmlPath;
CCLOG("Wtitablepath:%s",filePath.c_str());
TiXmlDocument * document=new TiXmlDocument(filepath.c_str());
document->LoadFile();
TiXmlElement * rootElement=document->RootElement();
TiXmlElement * ScenesElement=rootElement->FirstChildElement();
TiXmlElement * sceneElement=ScenesElement->FirstChildElement();
while (sceneElement) {
TiXmlElement * sceneNameElement=sceneElement->FirstChildElement();
TiXmlElement * sceneNumberElement=sceneNameElement->NextSiblingElement();
TiXmlElement * chapterNumElement=sceneNumberElement->NextSiblingElement();
TiXmlElement * unLockElement=chapterNumElement->NextSiblingElement();
TiXmlElement * needstarNumElement=unLockElement->NextSiblingElement();
int scenenumber=atoi(sceneNumberElement->GetText());
int getstarnumber=atoi(needstarNumElement->GetText());
if(getNumstar>=getstarnumber&&scenenumber==sceneNum+1)
{
TiXmlNode * oldNode=unLockElement->FirstChild();
char text[5];
sprintf(text, "%d",0);
TiXmlText newText(text);
unLockElement->ReplaceChild(oldNode, newText);
}
sceneElement=sceneElement->NextSiblingElement();
}
document->SaveFile();
}
void XMLParse:: updateChapterxml(int getNumstar,int _highestscore,int levelNum)
{
char name[20];
int scenenumber=CCUserDefault::sharedUserDefault()->getIntegerForKey("SceneNumber");
sprintf(name, "Chapter%d.xml",scenenumber);
std::string xmlPath=std::string(name);
FengZhuang::FileCopy(xmlPath.c_str());
std::string filePath=CCFileUtils::sharedFileUtils()->getWritablePath();
std::string filepath=filePath+xmlPath;
TiXmlDocument * document=new TiXmlDocument(filepath.c_str());
document->LoadFile();
TiXmlElement * rootElement=document->RootElement();
TiXmlElement * LevelsElement=rootElement->FirstChildElement();
TiXmlElement * levelElement=LevelsElement->FirstChildElement();
while (levelElement) {
TiXmlElement * levelNameElement=levelElement->FirstChildElement();
TiXmlElement * levelNumberElement=levelNameElement->NextSiblingElement();
int levelnumber=atoi(levelNumberElement->GetText());
TiXmlElement * unLockElement=levelNumberElement->NextSiblingElement();
TiXmlElement * starNumElement=unLockElement->NextSiblingElement();
int starnumber=atoi(starNumElement->GetText());
TiXmlElement * highestscoreElement=starNumElement->NextSiblingElement();
int highestscore=atoi(highestscoreElement->GetText()) ;
/*TiXmlNode * highestNode=highestscoreElement->FirstChild();
char text[10];
sprintf(text, "%d",0);
TiXmlText newText(text);
highestscoreElement->ReplaceChild(highestNode, newText);*/
if(levelnumber==levelNum)
{
if(_highestscore>highestscore)
{
TiXmlNode * highestNode=highestscoreElement->FirstChild();
char text[10];
sprintf(text, "%d",_highestscore);
TiXmlText newText(text);
highestscoreElement->ReplaceChild(highestNode, newText);
}
if(getNumstar>starnumber)
{
TiXmlNode * starnumberNode=starNumElement->FirstChild();
char text1[5];
sprintf(text1, "%d",getNumstar);
TiXmlText newText1(text1);
starNumElement->ReplaceChild(starnumberNode, newText1);
}
}
if (levelnumber==levelNum+1) {
TiXmlNode * nextlevelNode=unLockElement->FirstChild();
char text2[5];
sprintf(text2, "%d",0);
TiXmlText newText2(text2);
unLockElement->ReplaceChild(nextlevelNode, newText2);
}
levelElement=levelElement->NextSiblingElement();
}
document->SaveFile();
}