plist文件读取
1、在cocos2dx中支持的标签有: dict 、 key 、 integer 、 string 、 real、 true 、 false、array 示例: plist文件: <?xml version="1.0" encoding="UTF-8"?> 读取: { CCDictionary* plistDic = CCDictionary::createWithContentsOfFile("Images/test.plist"); } * cocos2dx 读取的数据中,所有数据类型都是转为CCString*, 然后添加到整个字典中,这个字典的节点类型是CCObject*。其中,true和false分别转为1和0,然后转CCString*,string、integer、real直接转为CCString*;所以使用字典中的数据时,要先从CCObject*转CCString*,然后从CCString*转其他类型。
2、一个跨平台的开源库,用于解析plist文件,使用了boost库,使用方法在README文件中 https://github.com/animetrics/PlistCpp 支持的类型在README中有阐述(dict 、 key 、 integer 、 string 、 real、 true 、 false、array、date、data) 示例: int _tmain(int argc,_TCHAR* argv[])
=================================== 1、上述两个库都可以再自己添加支持其它类型,反正有了源码了。 2、格式:dict 下的格式是key 然后类型,如: <dict> <key>key1<key> <string>hello world</string> </dict> array下的格式是没有key标签的,如: <array> <string>hello world!</string> <integer>250</integer> </array> 3、cocos2dx中plist格式的配置文件通常格式是这样子:
而且,必须要有<key>metadata</key> 部分,而且必须要有<key>format</key>,而且format的值必须为1才说明这个配置文件时有效的,否则配置文件不会被加载。 加载配置文件的方式: CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-test-ok.plist"); =================================== =================================== =================================== 由于XML文件在储存时不是最有空间效率的,Mac OS X 10.2导入了一种新的格式,它将plist文件存储为二进制文件。从Mac OS X 10.4开始,这是偏好设置文件的默认格式。 读写二进制格式的plist文件: int _tmain(int argc,_TCHAR* argv[]) { //读写XML格式plist文件 dictionary_type dict; Plist::readPlist("XMLExample1.plist",dict); for(dictionary_type::const_iterator it = dict.begin(); it != dict.end(); ++it) { cout<< "key: "<< it->first <<endl; } //读数组 const array_type& plistArray = boost::any_cast<const array_type&>(dict.find("testArray")->second); cout << boost::any_cast<const int64_t&>(plistArray[0]) << endl; cout << boost::any_cast<const string&>(plistArray[1]).c_str() << endl; //读字典 const dictionary_type& plistDic = boost::any_cast<const dictionary_type&>(dict.find("testDict")->second); dictionary_type::const_iterator it = plistDic.begin(); cout << "key:" << it->first << endl; cout << boost::any_cast<const string&>(it->second) << endl; //读一个变量 const Date& date = boost::any_cast<const Date&>(dict.find("testDate")->second); std::string strDate = date.timeAsXMLConvention(); /* 缺点:读取的数据类型不好动态判断,因为,读出的数据都放到了boost::any中了,类型无法认为识别。 */ //读写二进制格式plist文件 dictionary_type dic; dic.insert(std::make_pair("testArray",plistArray)); Plist::writePlistBinary("fileB",dic); dictionary_type dicR; Plist::readPlist("fileB",dicR); //读数组 const array_type& plistArrayR = boost::any_cast<const array_type&>(dicR.find("testArray")->second); cout << boost::any_cast<const int64_t&>(plistArrayR[0]) << endl; cout << boost::any_cast<const string&>(plistArrayR[1]).c_str() << endl; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |