http://www.2cto.com/kf/201402/280576.html
来源:互联网 发布:女友花钱大手大脚 知乎 编辑:程序博客网 时间:2024/04/30 08:20
Cocos2dx 3.0 提高篇(十一) xml文档的读取与调用
假设有一个名为 label.xml 的文档,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<plist version=
"1.0"
>
<dict>
<key>id</key>
<string>
10
</string>
<key>info</key>
<string>风一般的男纸</string>
</dict>
<dict>
<key>id</key>
<string>
20
</string>
<key>info</key>
<string>注定是寂寞的</string>
</dict>
</array>
</plist>
步骤如下:
1、读取xml文档,将读取到的内容放到ValueVector上。
2、通过id获取info里的内容;
3、将info里的内容显示到label中。
1
1
ValueVector txt_vec = FileUtils::getInstance()->getValueVectorFromFile(
"label.xml"
);
这里有两个要点,一个是ValueVector,这是啥东东?我只能回答在,在CCValue.h里,有这么一行代码 :
1
typedef std::vector<value> ValueVector;</value>
恩,人艰不拆;
第二个要点是用 getValueVectorFromFile(FileName)读取xml文档...
2、提取数据
首先提取 id ,因为id和它对应的值是一对键值,所以可以用Map来存储它们:
1
auto txt_map = txt_vec.at(
0
).asValueMap();
放到Map中即可用Map的方法读取键为”id"的值是多少:
1
int
id_int = txt_map.at(
"id"
).asInt();
最后就是做出判断,如果id的值为10的话,那么提取相应的键为 info 的值:
1
2
3
4
if
(id_int ==
10
)
{
auto label_str = txt_map.at(
"info"
).asString();
}
恩,过程就是这样;
3、将整理好的代码贴出来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ValueVector txt_vec = FileUtils::getInstance()->getValueVectorFromFile(
"label.xml"
);
//读取xml文档,放入ValueVector中
for
( auto& e : txt_vec)
{
auto txt_map = e.asValueMap();
//将键值转化成Map格式,放入txt_map中
int
id_int = txt_map.at(
"id"
).asInt();
//获取id
if
(
10
== id_int)
{
auto label_str = txt_map.at(
"info"
).asString();
//获取info的值
auto label1 = LabelTTF::create(label_str,
"Arial"
,
25
);
label1->setPosition(Point(
160
,
425
));
this
->addChild(label1,
2
);
}
else
if
(
20
== id_int)
{
auto label_str = txt_map.at(
"info"
).asString();
auto label1 = LabelTTF::create(label_str,
"Arial"
,
25
);
label1->setPosition(Point(
160
,
400
));
this
->addChild(label1,
2
);
}
}
如果有对Vector 、 Map使用不大了解的人,可以参考我之前写的博客:
Vector:http://blog.csdn.net/start530/article/details/19170853
Map:http://blog.csdn.net/start530/article/details/19284301
0 0
- http://www.2cto.com/kf/201402/280576.html
- http://www.2cto.com/kf/201112/115476.html
- http://www.2cto.com/kf/201205/131945.html
- http://www.2cto.com/kf/201410/344061.html
- http://www.2cto.com/kf/201311/255432.html
- android ContentResolver详解 http://www.2cto.com/kf/201207/144022.html
- ASP.NET MVC HtmlHelper用法大全(转:http://www.2cto.com/kf/201105/90985.html)
- argparse解析器 转自http://www.2cto.com/kf/201208/149418.html
- iframe跨域通信方法(转自http://www.2cto.com/kf/201211/166555.html)
- Android开发之MdiaPlayer详解 http://www.2cto.com/kf/201408/325309.html
- urllib2库 http://www.2cto.com/kf/201208/145070.html
- 使用HTTP协议访问网络 原网址https://www.2cto.com/kf/201701/552932.html
- Java生成二维码,转载!感觉不错Mark下(出处:http://www.2cto.com/kf/201108/98471.html)
- Java RandomAccessFile的使用(转载的文章,出处http://www.2cto.com/kf/201208/149816.html)
- 实现多线程的两种方法(转自:http://www.2cto.com/kf/201507/423032.html)
- 留作笔记,原文链接:https://www.2cto.com/kf/201607/529682.html
- http://www.2cto.com/database/201408/327315.html
- http://www.2cto.com/os/201202/118033.html
- 5.4.4 Java的常用包
- 使用ActionBar,调整Theme时遇到You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
- Oracle 表空间、用户、角色、授权
- 题5Longest Palindromic Substring
- LoaderManager.LoaderCallbacks使用实例
- http://www.2cto.com/kf/201402/280576.html
- HttpClient的使用
- Filter
- 云主机跟VPS哪个比较好?哪个稳定安全?
- 用exe4j将jar包转成.exe文件的教程
- Pascal's Triangle II
- Binary Tree Right Side View
- win7安装提示“缺少所需的CD/DVD驱动器设备驱动程序”
- Linux/Unix下使用tar命令压缩和解压[tar Examples]