在开发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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。