微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐