在Cocos2d-x中使用XML实例讲解

2015年03月23日 10:40 0 点赞 0 评论 更新于 2017-05-09 19:44

XML(可扩展标记语言)在游戏开发中应用广泛,常用于保存游戏信息,如最高分、游戏等级等,还可用于描述一些资源。在之前的开发中,我们就多次接触到XML文件。例如,在《使用CCAnimation创建动画》里,使用plist文件加载动画时,其实plist文件就是一个XML文件;在《在Cocos2d-x中使用瓦片地图(一)》和《在Cocos2d-x中使用瓦片地图(二)》中,使用瓦片地图编辑器创建的瓦片地图保存后得到的tmx格式文件,同样也是XML文件。

此外,XML文件还能解决中文乱码问题。在Cocos2d-X中出现中文编码乱码,是由于编码方式不同。在Windows环境下,通常使用VC作为IDE,而VC使用的是GTK编码,中文使用的是UTF - 8编码,这种差异导致在Cocos2d-X中直接使用中文会出现乱码。而XML使用的也是UTF - 8编码,因此使用XML可以实现在Cocos2d-X中正常显示中文。

接下来,我们通过一些实例详细介绍XML在Cocos2d-X中的应用。

程序实例1:使用CCUserDefault读取XML中的信息

实现步骤

  1. 创建一个XML文件
  2. 将最高分写入XML文件中
  3. 读取XML文件中的最高分

代码实现

创建XML文件

// 将游戏的最高分保存到内存中
CCUserDefault::sharedUserDefault()->setIntegerForKey("HighScore", 7000);
// 将游戏的最高分信息写到硬盘中
CCUserDefault::sharedUserDefault()->flush();

编译成功后,会在Debug目录下生成一个名为UserDefault.xml的文件。该文件内容如下:

<key>HighScore</key>
<integer>7000</integer>

读取UserDefault.xml中的最高分

// 读取游戏的最高分,当没有读取到时返回0
int highScore = CCUserDefault::sharedUserDefault()->getIntegerForKey("HighScore", 0);
// 打印最高分
CCLog("highScore=%d", highScore);

程序实例2:使用plist格式的XML文件保存用户信息并读取

实现步骤

  1. 新建一个格式为plist的XML文件,文件内容如下:
    <dict>
    <key>name</key>
    <string>张三丰</string>
    <key>age</key>
    <integer>36</integer>
    </dict>
    
  2. 在程序中添加代码读取信息:
    // 创建一个字典类,用于读取plist格式的xml文件
    CCDictionary* dict = CCDictionary::createWithContentsOfFile("aaa.plist");
    // 从aaa.plist中读取name的信息
    const CCString* name = dict->valueForKey("name");
    // 从aaa.plist中读取age的信息
    const CCString* age = dict->valueForKey("age");
    // 打印这两个信息
    CCLog("name is %s, age is %d", name->getCString(), age->intValue());
    

程序实例3:使用plist格式的xml文件保存复杂用户信息并读取

实现步骤

  1. 新建一个格式为plist的XML文件,文件内容如下:
    <dict>
    <key>name</key>
    <string>张三丰</string>
    <key>age</key>
    <integer>36</integer>
    <key>family</key>
    <dict>
    <key>son</key>
    <dict>
    <key>name</key>
    <string>xxx</string>
    <key>age</key>
    <integer>6</integer>
    </dict>
    <key>daughter</key>
    <dict>
    <key>name</key>
    <string>yyy</string>
    <key>age</key>
    <integer>3</integer>
    </dict>
    </dict>
    </dict>
    
  2. 在程序中添加代码读取信息:
    // 创建一个字典类,用于读取plist格式的xml文件
    CCDictionary* dict = CCDictionary::createWithContentsOfFile("aaa.plist");
    // 从aaa.plist中读取name的信息
    const CCString* name = dict->valueForKey("name");
    // 从aaa.plist中读取age的信息
    const CCString* age = dict->valueForKey("age");
    // 打印这两个信息
    CCLog("name is %s, age is %d", name->getCString(), age->intValue());
    // 从aaa.plist中读取family的信息
    CCObject* oFamily = dict->objectForKey("family");
    CCDictionary* dictFamily = (CCDictionary*)oFamily;
    // 在字典中查找son的信息
    CCDictionary* dictSon = (CCDictionary*)dictFamily->objectForKey("son");
    // 得到son的名字
    name = dictSon->valueForKey("name");
    // 得到son的年龄
    age = dictSon->valueForKey("age");
    // 打印son的信息
    CCLog("the name of son is %s, the age of son is %d", name->getCString(), age->intValue());
    // 在字典中查找daughter的信息
    CCDictionary* dictdaughter = (CCDictionary*)dictFamily->objectForKey("daughter");
    // 查找daughter的名字
    name = dictdaughter->valueForKey("name");
    // 查找daughter的年龄
    age = dictdaughter->valueForKey("age");
    // 打印daughter的信息
    CCLog("the name of daughter is %s, the age of daughter is %d", name->getCString(), age->intValue());
    

程序实例4:创建一个XML文件解析器

代码实现

#include "T40XML_tinyXML.h"

CCScene* T40XML_tinyXML::scene()
{
CCScene* s = CCScene::create();
T40XML_tinyXML* layer = T40XML_tinyXML::create();
s->addChild(layer);
return s;
}

void T40XML_tinyXML::WalkOver(tinyxml2::XMLElement* node)
{
// 获得根节点下的第一个子结点
tinyxml2::XMLElement* curNode = node->FirstChildElement();
// 遍历xml中的结点
while (curNode)
{
if(curNode->FirstChild())
{
CCLog("node is %s, value is %s", curNode->Value(), curNode->FirstChild()->Value());
}
else
{
CCLog("node is %s, value is NULL", curNode->Value());
}
WalkOver(curNode);
curNode = curNode->NextSiblingElement();
}
}

bool T40XML_tinyXML::init()
{
CCLayer::init();
// 创建一个xml文档
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
// 加载xml文档
doc->LoadFile("aaa.plist");
// 打印doc中所有的的内容
tinyxml2::XMLElement* rootElement = doc->RootElement();
// 打印aaa.plist中根节点上的内容
CCLog("rootElemenet value is %s", rootElement->Value());
// 打印aaa.plist中所有结点的信息
WalkOver(rootElement);
delete doc;
return true;
}

通过以上实例,我们详细展示了XML在Cocos2d-X中的多种应用场景,包括保存和读取游戏信息、解决中文乱码以及创建XML解析器等。希望这些实例能帮助你更好地理解和使用XML在Cocos2d-X中的开发。

作者信息

menghao

menghao

共发布了 332 篇文章