plist文件读取

来源:互联网 发布:100寸投影幕 布 淘宝 编辑:程序博客网 时间:2024/05/01 19:34

1、在cocos2dx中支持的标签有:

dict 、 key integer 、 string 、 real true 、 falsearray

示例:

plist文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key1</key>
<string>hello world!</string>
<key>key2</key>
<integer>22</integer>
<key>key3</key>
<real>2.22</real>
<key>key4</key>
<false/>
<key>key5</key>
<array>
<integer>33</integer>
<string>hello world!</string>
</array>
</dict>
</plist>

读取:

{

    CCDictionary* plistDic = CCDictionary::createWithContentsOfFile("Images/test.plist");
    std::string value1 = plistDic->valueForKey("key1")->getCString();
    int value2 = plistDic->valueForKey("key2")->intValue();
    float value3 = plistDic->valueForKey("key3")->floatValue();
    bool value4 = plistDic->valueForKey("key4")->boolValue();
    CCArray* arr = (CCArray*)(plistDic->objectForKey("key5"));
    
    for (int i = 0; i < arr->count(); i++)
    {
        CCLog("%s", ((CCString*)(arr->objectAtIndex(i)))->getCString());
    }
 
    //CCArray* brr = dynamic_cast<CCArray*>(plistDic->objectForKey("key5"));

}

* 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 、 falsearray、date、data

示例:

int _tmain(int argc, _TCHAR* argv[])
{
    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中了,类型无法认为识别。
    */

    return 0;
}


===================================

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;
}

0 0
原创粉丝点击