您好,登錄后才能下訂單哦!
在開發(fā)cocos2dx跨平臺(tái)網(wǎng)絡(luò)游戲的時(shí)候,數(shù)據(jù)基本都會(huì)處理成xml格式。
cocos2dx封裝了libxml,處理xml可以使用封裝好的類CCSAXParser,
采用key - value ,key- value方式編輯的xml可以采用CCSAXParser。
我們編輯的xml大量使用到了key-value -Attribute ,就是數(shù)據(jù)基本都寫在了屬性里面,所以我采用了第三方庫(kù)tinyxml
下面是代碼實(shí)現(xiàn):
- void HXmlParse::Read_questxml(const char * docname , char* buf_ ,GameBoard * gameBoardinstance){
- TiXmlDocument doc(docname);
- //bool loadOkay = doc.LoadFile();
- if( strcmp( docname,"")!=0){
- //這個(gè)是從網(wǎng)絡(luò)下載的文件,緩存到本地,所以有判斷
- char* pBytes = GOEUtilies::getFileDataFromWriteablePath(docname);
- // CCLog("getFileData:%s |pBytes:%d",docname,size);
- doc.Parse((const char *)pBytes,0,TIXML_ENCODING_UTF8);
- }else
- {
- doc.Parse((const char*)buf_,0,TIXML_ENCODING_UTF8);
- }
- // if ( !loadOkay )
- // {
- // CCLOG( "Could not load test file '%s'. Error='%s'. Exiting.\n", docname,doc.ErrorDesc() );
- // return;
- // }
- TiXmlNode* noderoot = 0; //the root node
- //TiXmlNode* nodearray = 0;
- TiXmlElement* todoElement = 0; // the first node , like Accout,Crop ...
- TiXmlElement* itemElement = 0; // the Accout,Crop 's element list
- noderoot = doc.RootElement()->FirstChild();
- gameBoardinstance->_goeStaticData->quests_ = new CCDictionary();
- for( todoElement = noderoot->FirstChildElement();
- todoElement;
- todoElement = todoElement->NextSiblingElement())
- {
- GOEStaticQuest * quest = new GOEStaticQuest();
- quest->questId_ = atoi(todoElement->Attribute("id"));
- gameBoardinstance->_goeStaticData->questId_ = quest->questId_;
- quest->needIds_ = *(CCString::create(todoElement->Attribute("needid")));
- quest->nextIds_ = *(CCString::create(todoElement->Attribute("nextid")));
- quest->newId_ = atoi(todoElement->Attribute("newid"));
- quest->exp_ = atoi(todoElement->Attribute("exp"));
- quest->resource_ = atoi(todoElement->Attribute("recource"));
- quest->coin_ = atoi(todoElement->Attribute("coin"));
- quest->card_ = *(CCString::create(todoElement->Attribute("card")));
- quest->icon_ = *(CCString::create(todoElement->Attribute("icon")));
- quest->power_ = atoi(todoElement->Attribute("power"));
- gameBoardinstance->_goeStaticData->quests_->setObject((CCObject*)quest, quest->questId_);
- for( itemElement = todoElement->FirstChildElement();
- itemElement;
- itemElement = itemElement->NextSiblingElement() )
- {
- GOEStaticTask * task = new GOEStaticTask();
- task->taskId_ = atoi(itemElement->Attribute("id"));
- task->action_ = *(CCString::create(itemElement->Attribute("action")));
- task->type_ = *(CCString::create(itemElement->Attribute("type")));
- task->value_ = *(CCString::create(itemElement->Attribute("value")));
- task->params_ = *(CCString::create(itemElement->Attribute("params")));
- GOEStaticQuest * q = (GOEStaticQuest*)gameBoardinstance->_goeStaticData->quests_->objectForKey(gameBoardinstance->_goeStaticData->questId_);
- q->tasks_.addObject((CCObject*)task);
- }
- }
- }
可以對(duì)比xml,很容易理解代碼,主要就是節(jié)點(diǎn),然后遍歷
- <?xml version='1.0' encoding='UTF-8' standalone='no'?>
- <goe:database xmlns:goe='http://www.elex-tech.com/sns/platform/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
- <quests>
- <quest id="301" icon="quest2" newid="0" needid="202" nextid="401" exp="0" recource="0" coin="0" card="0" power="0">
- <task id="0" action="buildingHarvest" type="301" value="0" params="0"/>
- </quest>
- <quest id="302" icon="quest2" newid="0" needid="204" nextid="402" exp="0" recource="0" coin="0" card="0" power="0">
- <task id="0" action="buildingHarvest" type="401" value="0" params="0"/>
- </quest>
- </quests>
- </goe:database>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。