cocos2dx中.json和.plist以及.xml文件格式生成加载的不同

一、.json加载,对于下列形式

[["key","value"]

,["hot_update_version",0]

,["totallv",120]

,["releaseversion",1.5]

]

一般通过std::string data =FileUtils::getInstance()->getStringFromFile(filename); 通过文本rapidjson::Document doc;

doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str()); 解析data。

解析之后doc应该是一个数组形式的,而且是一个二维数组(或者说是一个矩阵形式)。

如果解析没有错误,可以通过doc.size()取得总行数,然后通过rapidjson::Value &v=doc[i]取得每一列的值,它也是一个数组,v.size()取得总列数,通过const auto& value = v[index];取得具体的值,这个值可能是NULL,可能是int,可能是string,可以通过value.IsNull()、value.IsString()、value.IsInt()判断并通过value.GetString()或value.GetInt()等获取该值。

对于下列形式

"snow" : [

[ 2,3,7 ],

[ 2,8 ],

[ 3,7,7 ]

],

"cirrus" : [

[ 4,4 ]

],

"down" : [ 1,2,4,5,6 ],

"target" : [

[2,1,3]

],

"targetScore" : 1000,

"moves" : 15

一般通过

//获取行数

static const string tileKey = "snow";

auto rowLen = DICTOOL->getArrayCount_json(json,tileKey.c_str());

for (int row = 0; row < rowLen; ++row){

//获取行的值

const auto& rowValue = DICTOOL->getDictionaryFromArray_json(json,tileKey.c_str(),row);

//遍历行的值

for (rapidjson::SizeType col = 0; col < rowValue.Size(); ++col ){

//取得具体值

if ( rowValue[col].GetInt() ){

}else{

}

}

}

const auto& moves = DICTOOL->getIntValue_json(json,"moves",0);

_moveCounting = moves;



.json文件的生成,通过定义Json::Value root,Json::Valueitem,item.append(1),item.append(2),

root["test"].append(item),最终通过

// 缩进输出

Json::StyledWriter sw;

// 输出到文件

ofstream os;

os.open(sLevelPath);

os << sw.write(root);

os.close();

二、.plist加载,一般通过ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(filename);获取最外层的ValueMap,该valuemap里面可以嵌套其他valuemap,获取内部嵌套的其他valuemap,可以这样获取,auto dataIter =dict.find("data"),(此时dataIter相当于一个Value),通过判断if(dataIter != dict.cend()&&dataIter->second.getType()==Value::Type::MAP){const auto& data = dataIter->second.asValueMap();}

.plist生成,一般通过

/*

* Use tinyxml2 to write plist files

*/

bool FileUtils::writeToFile(ValueMap& dict,const std::string &fullPath)

三、.xml加载,

函数static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey,tinyxml2::XMLElement** rootNode,tinyxml2::XMLDocument **doc)


tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();

*doc = xmlDoc;


std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());


if (xmlBuffer.empty())

{

CCLOG("can not read xml file");

break;

}

xmlDoc->Parse(xmlBuffer.c_str(),xmlBuffer.size());


// get root node

*rootNode = xmlDoc->RootElement();

if (nullptr == *rootNode)

{

CCLOG("read root node error");

break;

}

// find the node

curNode = (*rootNode)->FirstChildElement();

while (nullptr != curNode)

{

const char* nodeName = curNode->Value();

if (!strcmp(nodeName,pKey))

{

break;

}


curNode = curNode->NextSiblingElement();

}


if (curNode)

{

if (curNode->FirstChild())

{

curNode->FirstChild()->SetValue(pValue);

}

else

{

tinyxml2::XMLText* content = doc->NewText(pValue);

curNode->LinkEndChild(content);

}

}

else

{

if (rootNode)

{

tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);

rootNode->LinkEndChild(tmpNode);

tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);

tmpNode->LinkEndChild(content);

}

}

//.xml生成

// save file and free doc

if (doc)

{

doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());

delete doc;

}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...