您好,登錄后才能下訂單哦!
在開發cocos2dx跨平臺網絡游戲的時候,數據基本都會處理成xml格式。
cocos2dx封裝了libxml,處理xml可以使用封裝好的類CCSAXParser,
采用key - value ,key- value方式編輯的xml可以采用CCSAXParser。
我們編輯的xml大量使用到了key-value -Attribute ,就是數據基本都寫在了屬性里面,所以我采用了第三方庫tinyxml
下面是代碼實現:
- void HXmlParse::Read_questxml(const char * docname , char* buf_ ,GameBoard * gameBoardinstance){
- TiXmlDocument doc(docname);
- //bool loadOkay = doc.LoadFile();
- if( strcmp( docname,"")!=0){
- //這個是從網絡下載的文件,緩存到本地,所以有判斷
- 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);
- }
- }
- }
可以對比xml,很容易理解代碼,主要就是節點,然后遍歷
- <?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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。