Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于external/json下。
rapidjson 项目地址:http://code.google.com/p/rapidjson/wiki:http://code.google.com/p/rapidjson/wiki/UserGuide
下面就通过实例代码讲解rapidjson的用法。
使用rapidjson解析json串
- 引入头文件
12
#include "json/rapidjson.h"
#include "json/document.h"
- json解析
12
3
4
5
6
7
8
9
10
11
12
13
std::string str =
"{\"hello\" : \"word\"}"
;
CCLOG(
"%s\n"
, str.c_str());
rapidjson::Document d;
d.Parse<0>(str.c_str());
if
(d.HasParseError())
//打印解析错误
{
CCLOG(
"GetParseError %s\n"
,d.GetParseError());
}
if
(d.IsObject() && d.HasMember(
"hello"
)) {
CCLOG(
"%s\n"
, d[
"hello"
].GetString());
//打印获取hello的值
}
- 打印结果
12
3
cocos2d: {
"hello"
:
"word"
}
cocos2d: word
注意:只支持标准的json格式,一些非标准的json格式不支持。
一些常用的解析方法需要自己封装。注意判断解析节点是否存在。
使用rapidjson生成json串
- 引入头文件
12
3
4
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
using
namespace
rapidjson;
- 生成json串
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value array(rapidjson::kArrayType);
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember(
"int"
, 1, allocator);
object.AddMember(
"double"
, 1.0, allocator);
object.AddMember(
"bool"
,
true
, allocator);
object.AddMember(
"hello"
,
"你好"
, allocator);
array.PushBack(object, allocator);
document.AddMember(
"json"
,
"json string"
, allocator);
document.AddMember(
"array"
, array, allocator);
StringBuffer buffer;
rapidjson::Writer<StringBuffer> writer(buffer);
document.Accept(writer);
CCLOG(
"%s"
,buffer.GetString());
- 打印结果
1cocos2d: {
"json"
:
"json string"
,
"array"
:[{
"int"
:1,
"double"
:1,
"bool"
:
true
Cocos2d-x 已经加入了
tinyxml2
用于xml的解析。3.0版本位于external/tinyxml2
下。2.x版本位于cocos2dx/support/tinyxml2
下。tinyxml2 Github地址:https://github.com/leethomason/tinyxml2
帮助文档地址:http://grinninglizard.com/tinyxml2docs/index.html
生成xml文档
- 引入头文件
12
#include "tinyxml2/tinyxml2.h"
using
namespace
tinyxml2;
- xml文档生成
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void
HelloWorld::makeXML(
const
char
*fileName)
{
std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
XMLDocument *pDoc =
new
XMLDocument();
//xml 声明(参数可选)
XMLDeclaration *pDel = pDoc->NewDeclaration(
"xml version=\"1.0\" encoding=\"UTF-8\""
);
pDoc->LinkEndChild(pDel);
//添加plist节点
XMLElement *plistElement = pDoc->NewElement(
"plist"
);
plistElement->SetAttribute(
"version"
,
"1.0"
);
pDoc->LinkEndChild(plistElement);
XMLComment *commentElement = pDoc->NewComment(
"this is xml comment"
);
plistElement->LinkEndChild(commentElement);
//添加dic节点
XMLElement *dicElement = pDoc->NewElement(
"dic"
);
plistElement->LinkEndChild(dicElement);
//添加key节点
XMLElement *keyElement = pDoc->NewElement(
"key"
);
keyElement->LinkEndChild(pDoc->NewText(
"Text"
));
dicElement->LinkEndChild(keyElement);
XMLElement *arrayElement = pDoc->NewElement(
"array"
);
dicElement->LinkEndChild(arrayElement);
for
(
int
i = 0; i<3; i++) {
XMLElement *elm = pDoc->NewElement(
"name"
);
elm->LinkEndChild(pDoc->NewText(
"Cocos2d-x"
));
arrayElement->LinkEndChild(elm);
}
pDoc->SaveFile(filePath.c_str());
pDoc->Print();
delete
pDoc;
}
- 打印结果
12
3
4
5
6
7
8
9
10
11
12
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<plist version=
"1.0"
>
<!--
this
is xml comment-->
<dic>
<key>Text</key>
<array>
<name>Cocos2d-x</name>
<name>Cocos2d-x</name>
<name>Cocos2d-x</name>
</array>
</dic>
</plist>
上面代码使用tinyxml简单生成了一个xml文档。
解析xml
下面我们就来解析上面创建的xml文档
- 引入头文件
12
#include "tinyxml2/tinyxml2.h"
using
namespace
tinyxml2;
- xml解析
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void
HelloWorld::parseXML(
const
char
*fileName)
{
std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
XMLDocument *pDoc =
new
XMLDocument();
XMLError errorId = pDoc->LoadFile(filePath.c_str());
if
(errorId != 0) {
//xml格式错误
return
;
}
XMLElement *rootEle = pDoc->RootElement();
//获取第一个节点属性
const
XMLAttribute *attribute = rootEle->FirstAttribute();
//打印节点属性名和值
log
(
"attribute_name = %s,attribute_value = %s"
, attribute->Name(), attribute->Value());
XMLElement *dicEle = rootEle->FirstChildElement(
"dic"
);
XMLElement *keyEle = dicEle->FirstChildElement(
"key"
);
if
(keyEle) {
log
(
"keyEle Text= %s"
, keyEle->GetText());
}
XMLElement *arrayEle = keyEle->NextSiblingElement();
XMLElement *childEle = arrayEle->FirstChildElement();
while
( childEle ) {
log
(
"childEle Text= %s"
, childEle->GetText());
childEle = childEle->NextSiblingElement();
}
delete
pDoc;
}
在节点解析过程中,注意对获取到的节点进行判空处理。
- 解析结果打印
12
3
4
5
cocos2d: attribute_name = version,attribute_value = 1.0
cocos2d: keyEle Text= Text
cocos2d: childEle Text= Cocos2d-x
cocos2d: childEle Text= Cocos2d-x
cocos2d: childEle Text= Cocos2d-x
小结
上面的简单示例,演示了如何使用tinyxml进行xml文档生成和解析。更多详细的帮助请参考 tinyxml帮助文档http://grinninglizard.com/tinyxml2docs/index.html
- 引入头文件