XML就可以扩展标记语言。在游戏开发中,经常使用于保存游戏信息,如最高分,游戏等级。等信息,和描写叙述一些资源等,我第一次使用xml是在使用CCAnimation创建动画中,使用plist文件载入动画时。使用了xml文件当中plist文件事实上就是一个xml文件,在前面的博客中的在Cocos2d-X中使用瓦片地图《一》在Cocos2d-X中使用瓦片地图《二》中使用瓦片地图编辑器创建的瓦片地图保存后会得到一个tmx格式的文件,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的xml文件

UserDefault.xml中的代码:UserDefault.xml中有一个最高分

<?xml version="1.0" encoding="UTF-8"?>

-<userDefaultRoot>

<HighScore>7000</HighScore>

</userDefaultRoot>

读取UserDefault.xml中的最高分

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

运行结果:

程序实例2:使用plist格式的xml文件保存用户信息,而且读取用户信息1

实现过程:

新建一个格式为plist的xml文件,文件里的内容例如以下

<?

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>name</key>
<string>张三丰</string>
<key>age</key>
<integer>36</integer> </dict>
</plist>

在程序中加入代码

    //创建一个字典类。用于读取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文件保存用户信息。而且读取用户信息2

实现过程:

新建一个格式为plist的xml文件,文件里的内容例如以下

<?

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>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>
</plist>

在程序中加入以下的代码:

  //创建一个字典类。用于读取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;
}

运行结果:




最新文章

  1. Cordova 3.x入门 - 目录
  2. 优化mysql服务器
  3. 转战网站后台与python
  4. php中日期的加减法运算
  5. 235. Lowest Common Ancestor of a Binary Search Tree
  6. Oracle数据库备份与恢复的常用方法
  7. oracle database resident connection pooling(驻留连接池)
  8. android http协议post请求方式
  9. HDMI相关知识
  10. 【LeetCode练习题】Minimum Window Substring
  11. IO库 8.2
  12. 识别率很高的java文字识别技术
  13. 以ActiveMQ为例JAVA消息中间件学习【1】
  14. PID控制器(比例-积分-微分控制器)- II
  15. python 计时器
  16. java中基础数据类型的应用
  17. split和strip的使用
  18. POJ 1182 并查集
  19. 【数据库】MFC ODBC(一)
  20. Dropwizard框架入门

热门文章

  1. Unity 网络请求(1)
  2. Appium+python自动化55-appium desktop每次启动安装Unlock和Appium Setting问题
  3. SQL中关于where后面不能放聚合函数(如sum等)的解决办法
  4. android 设置屏幕方向
  5. Hibernate中@Embedded和@Embeddable注解
  6. error: &lt;class &#39;xml.parsers.expat.ExpatError&#39;&gt;, syntax error: line 1, column 0: file: /usr/local/lib/python2.7/xmlrpclib.py line: 557
  7. Zookeeper常用命令 (转)
  8. 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
  9. 从AIDL开始谈Android进程间Binder通信机制
  10. ActiveMQ API 详解