<1>XMLParse.h
#ifndef __MyEliminate__XMLParser__ #define __MyEliminate__XMLParser__ #include <stdio.h> #include "cocos2d.h" #include <fstream> using namespace cocos2d; using namespace std; class XMLParser { public: ~XMLParser(); static XMLParser *getXMLParser(); void writeToDocPath(); void levelXMLParser(); void updateLevelXML(int level); void setCurrentStars(int stars); int getStars(); void setLevel(int num); int getLevel(); private: XMLParser(); public: ValueVector m_unLockValueVector; private: static XMLParser *m_xmlParser; static string m_levelDocPath; int m_stars; int m_level; //本关卡过完,得到评价的星星数目,设置一下,然后根据数目调用updateLevelXML方法就会自动更新 public: void updateStarNumByNewStarNumAndLevel(int newStarNum,int level); //根据新的星星数目和关卡 来 更新关卡信息 vector<int> _vecStars; int getStarNumByLevel(int level); //得到某关以前获取的星星数目 public: void unlockLevel(int level); //解锁level关卡 CC_SYNTHESIZE(int,_score,score); public: int countStarNum(); //统计所有关卡已经得到的分数 int getGuanqiaNum(); }; #endif /* defined(__MyEliminate__XMLParser__) */
<2>XMLParse.cpp
#include "XMLParser.h" #include "tinyxml.h" string XMLParser::m_levelDocPath; XMLParser *XMLParser::m_xmlParser = NULL; XMLParser *XMLParser::getXMLParser() { if (m_xmlParser == NULL) { m_xmlParser = new XMLParser; m_levelDocPath = FileUtils::getInstance()->getWritablePath() + "level.xml"; //m_levelDocPath = FileUtils::getInstance()->fullPathForFilename("level.xml"); //--jn } return m_xmlParser; } #pragma mark 将文件写入沙盒路径下 void XMLParser::writeToDocPath() { string immutableXMLPath = FileUtils::getInstance()->fullPathForFilename("level.xml"); if (!FileUtils::getInstance()->isFileExist(m_levelDocPath)) { string filePath = FileUtils::getInstance()->fullPathFromRelativeFile(immutableXMLPath,""); // long size; ssize_t size; char *pFileContent = (char *)FileUtils::getInstance()->getFileData(filePath,"r",&size); FILE *file = fopen(m_levelDocPath.c_str(),"w"); if (file) { fputs(pFileContent,file); fclose(file); } } } #pragma mark 解析关卡 void XMLParser::levelXMLParser() { m_unLockValueVector.clear(); // _vecStars.clear(); TiXmlDocument *myDocument = new TiXmlDocument(m_levelDocPath.c_str()); myDocument->LoadFile(); TiXmlElement *rootElememt = myDocument->RootElement(); //RSS TiXmlElement *levelsElement = rootElememt->FirstChildElement(); //Levels TiXmlElement *levelElement = levelsElement->FirstChildElement(); //level while (levelElement) { TiXmlElement *levelNameElement = levelElement->FirstChildElement(); TiXmlElement *levelIDElement = levelNameElement->NextSiblingElement(); TiXmlElement *unLockElement = levelIDElement->NextSiblingElement(); //关卡解锁 int unLock = atoi(unLockElement->GetText()); m_unLockValueVector.push_back(Value(unLock)); TiXmlElement *highestscoreElement = unLockElement->NextSiblingElement(); TiXmlElement *starsElement = highestscoreElement->NextSiblingElement(); //星星数目 int stars = atoi(starsElement->GetText()); _vecStars.push_back(stars); //下一个 levelElement = levelElement->NextSiblingElement(); } delete myDocument; } void XMLParser::updateLevelXML(int level) { TiXmlDocument *myDocument = new TiXmlDocument(m_levelDocPath.c_str()); myDocument->LoadFile(); TiXmlElement *rootElememt = myDocument->RootElement(); TiXmlElement *levelsElememt = rootElememt->FirstChildElement(); TiXmlElement *levelElement = levelsElememt->FirstChildElement(); while (levelElement) { TiXmlElement *levelNameElement = levelElement->FirstChildElement(); TiXmlElement *levelIDElement = levelNameElement->NextSiblingElement(); int levelID = atoi(levelIDElement->GetText()); TiXmlElement *unLockElement = levelIDElement->NextSiblingElement(); TiXmlElement *hightscoreElement = unLockElement->NextSiblingElement(); TiXmlElement *starsElement = hightscoreElement->NextSiblingElement(); int stars = atoi(starsElement->GetText()); if (levelID == level && m_stars > stars) { char text[20]; sprintf(text,"%d",m_stars); TiXmlText newText(text); TiXmlNode *oldNode = starsElement->FirstChild(); starsElement->ReplaceChild(oldNode,newText); break; } levelElement = levelElement->NextSiblingElement(); } myDocument->SaveFile(); delete myDocument; } //星星数 void XMLParser::setCurrentStars(int stars) { m_stars = stars; } int XMLParser::getStars() { return m_stars; } //关卡 void XMLParser::setLevel(int num) { m_level = num; } int XMLParser::getLevel() { return m_level; } XMLParser::XMLParser() { m_level = 1; _score = 0; } XMLParser::~XMLParser() { } void XMLParser::updateStarNumByNewStarNumAndLevel(int newStarNum,int level){ setCurrentStars(newStarNum); updateLevelXML(level); } int XMLParser::getStarNumByLevel(int level){ CCAssert(level < (int)_vecStars.size(),"关卡越界"); return _vecStars.at(level); } void XMLParser::unlockLevel(int level){ TiXmlDocument *myDocument = new TiXmlDocument(m_levelDocPath.c_str()); myDocument->LoadFile(); TiXmlElement *rootElememt = myDocument->RootElement(); TiXmlElement *levelsElememt = rootElememt->FirstChildElement(); TiXmlElement *levelElement = levelsElememt->FirstChildElement(); while (levelElement) { TiXmlElement *levelNameElement = levelElement->FirstChildElement(); TiXmlElement *levelIDElement = levelNameElement->NextSiblingElement(); int levelID = atoi(levelIDElement->GetText()); TiXmlElement *unLockElement = levelIDElement->NextSiblingElement(); // TiXmlElement *hightscoreElement = unLockElement->NextSiblingElement(); // TiXmlElement *starsElement = hightscoreElement->NextSiblingElement(); int unlock = atoi(unLockElement->GetText()); if (levelID == level && unlock == 1) { char text[20]; sprintf(text,0); TiXmlText newText(text); TiXmlNode *oldNode = unLockElement->FirstChild(); unLockElement->ReplaceChild(oldNode,newText); break; } levelElement = levelElement->NextSiblingElement(); } myDocument->SaveFile(); delete myDocument; } int XMLParser::countStarNum(){ int starNum = 0; TiXmlDocument *myDocument = new TiXmlDocument(m_levelDocPath.c_str()); myDocument->LoadFile(); TiXmlElement *rootElememt = myDocument->RootElement(); TiXmlElement *levelsElememt = rootElememt->FirstChildElement(); TiXmlElement *levelElement = levelsElememt->FirstChildElement(); while (levelElement) { TiXmlElement *levelNameElement = levelElement->FirstChildElement(); TiXmlElement *levelIDElement = levelNameElement->NextSiblingElement(); int levelID = atoi(levelIDElement->GetText()); TiXmlElement *unLockElement = levelIDElement->NextSiblingElement(); TiXmlElement *hightscoreElement = unLockElement->NextSiblingElement(); TiXmlElement *starsElement = hightscoreElement->NextSiblingElement(); int stars = atoi(starsElement->GetText()); if(!stars){ break; //如果某关卡得到的星星数为0,则后面的不再做检测了。 } //统计星星 starNum += stars; levelElement = levelElement->NextSiblingElement(); } myDocument->SaveFile(); delete myDocument; log("<<<当前所有关卡得到的星星总数是: %d",starNum); return starNum; } int XMLParser::getGuanqiaNum(){ int guanqiaNum = 0; TiXmlDocument *myDocument = new TiXmlDocument(m_levelDocPath.c_str()); myDocument->LoadFile(); TiXmlElement *rootElememt = myDocument->RootElement(); TiXmlElement *levelsElememt = rootElememt->FirstChildElement(); TiXmlElement *levelElement = levelsElememt->FirstChildElement(); while (levelElement) { guanqiaNum += 1; levelElement = levelElement->NextSiblingElement(); } myDocument->SaveFile(); delete myDocument; return guanqiaNum; }<3>例子1
<Class name="计算机软件班"> <Students> <student name="张三" studentNo="13031001" sex="男" age="22"> <phone>88208888</phone> <address>西安市太白南路二号</address> </student> <student name="李四" studentNo="13031002" sex="男" age="20"> <phone>88206666</phone> <address>西安市光华路</address> </student> </Students> </Class> #include <iostream> #include <string> #include <tinyxml.h> using std::string; int main() { TiXmlDocument* myDocument = new TiXmlDocument(); myDocument->LoadFile("Students.xml"); TiXmlElement* rootElement = myDocument->RootElement(); //Class TiXmlElement* studentsElement = rootElement->FirstChildElement(); //Students TiXmlElement* studentElement = studentsElement->FirstChildElement(); //Students while ( studentElement ) { TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute(); //获得student的name属性 while ( attributeOfStudent ) { std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl; attributeOfStudent = attributeOfStudent->Next(); } TiXmlElement* phoneElement = studentElement->FirstChildElement();//获得student的phone元素 std::cout << "phone" << " : " << phoneElement->GetText() << std::endl; TiXmlElement* addressElement = phoneElement->NextSiblingElement(); std::cout << "address" << " : " << phoneElement->GetText() << std::endl; studentElement = studentElement->NextSiblingElement(); } return 0; } name : 张三 studentNo : 13031001 sex : 男 age : 22 phone : 88208888 address : 88208888 name : 李四 studentNo : 13031002 sex : 男 age : 20 phone : 88206666 address : 88206666<4>例子2
string xmlString = "<Class name=\"计算机软件班\">\ <Students>\ <student name=\"张三\" studentNo=\"13031001\" sex=\"男\" age=\"22\">\ <phone>88208888</phone>\ <address>西安市太白南路二号</address>\ </student>\ <student name=\"李四\" studentNo=\"13031002\" sex=\"男\" age=\"20\">\ <phone>88206666</phone>\ <address>西安市光华路</address>\ </student>\ </Students>\ </Class>"; 或者用parse方法进行解析 // TiXmlDocument* myDocument = new TiXmlDocument(); myDocument->Parse(xmlString.c_str());