Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析
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解析
12345678910111213std::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的值}打印结果
123cocos2d: {"hello":"word"}cocos2d: word
注意:只支持标准的json格式,一些非标准的json格式不支持。
一些常用的解析方法需要自己封装。注意判断解析节点是否存在。
使用rapidjson生成json串
引入头文件
1234#include "json/document.h"#include "json/writer.h"#include "json/stringbuffer.h"usingnamespacerapidjson;生成json串
12345678910111213141516171819rapidjson::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":trueCocos2d-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"usingnamespacetinyxml2;xml文档生成
123456789101112131415161718192021222324252627282930313233343536373839404142voidHelloWorld::makeXML(constchar*fileName){std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;XMLDocument *pDoc =newXMLDocument();//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(inti = 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();deletepDoc;}打印结果
123456789101112<?xml version="1.0"encoding="UTF-8"?><plist version="1.0"><!--thisis 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"usingnamespacetinyxml2;xml解析
1234567891011121314151617181920212223242526272829303132333435voidHelloWorld::parseXML(constchar*fileName){std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;XMLDocument *pDoc =newXMLDocument();XMLError errorId = pDoc->LoadFile(filePath.c_str());if(errorId != 0) {//xml格式错误return;}XMLElement *rootEle = pDoc->RootElement();//获取第一个节点属性constXMLAttribute *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();}deletepDoc;}在节点解析过程中,注意对获取到的节点进行判空处理。
解析结果打印
12345cocos2d: attribute_name = version,attribute_value = 1.0cocos2d: keyEle Text= Textcocos2d: childEle Text= Cocos2d-xcocos2d: childEle Text= Cocos2d-xcocos2d: childEle Text= Cocos2d-x
小结
上面的简单示例,演示了如何使用tinyxml进行xml文档生成和解析。更多详细的帮助请参考 tinyxml帮助文档http://grinninglizard.com/tinyxml2docs/index.html
Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析的更多相关文章
- (27)Cocos2d-x 3.0 Json用法
Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...
- UI进阶 解析XML 解析JSON
1.数据解析 解析的基本概念 所谓“解析”:从事先规定好的格式中提取数据 解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照格式获取数据 iOS开发常见的解析:XML解析.JSO ...
- 高屋建瓴 cocos2d-x-3.0架构设计 Cocos2d (v.3.0) rendering pipeline roadmap(原文)
Cocos2d (v.3.0) rendering pipeline roadmap Why (the vision) The way currently Cocos2d does rendering ...
- C++通过jsoncpp类库读写JSON文件-json用法详解
介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...
- JSON: JSON 用法
ylbtech-JSON: JSON 用法 1. JSON Object creation in JavaScript返回顶部 1. <!DOCTYPE html> <html> ...
- .NET3.5中JSON用法以及封装JsonUtils工具类
.NET3.5中JSON用法以及封装JsonUtils工具类 我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...
- linux find命令中-print0和xargs中-0的用法
linux find命令中-print0和xargs中-0的用法. 1.默认情况下, find命令每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此find 的输出都是一行一行的: ...
- json用法常见错误
Json用法三个常见错误 net.sf.json.JSONException: java.lang.NoSuchMethodException
- 问题:c# newtonsoft.json使用;结果:Newtonsoft.Json 用法
Newtonsoft.Json 用法 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: http://json.codeplex.com/ 使用方法 ...
随机推荐
- js009-客户端检测
js009-客户端检测 本章内容: 1.使用能力检测 2.用户代理检测的历史 3.选择检测方式 一般不使用客户端检测. 9.1 能力检测 也称特性检测.基本模式如下: if(object.proper ...
- IBatis 构建 In语句
语句 <select id="qryProjectsByIds" parameterClass="Hashtable" resultMap="p ...
- photobooth.js jquery
<div id="example" class="photobooth" style="width:758px;height:400px&quo ...
- 从零开始HTML(一 2016/10/17)
就是准备跟着霹雳猿教程过一遍HTML啦,边看边记录更便于理解记忆吧~ 1.属性 HTML 标签可以拥有属性.属性提供了有关 HTML 元素的更多的信息.属性总是以名称/值对的形式出现,比如:name= ...
- javascript函数的定义与执行
要理解javascript函数的定义与执行,首先需要知道这几个重要的概念,现在可以先知道稍后再理解! 函数的执行环境(excution context).活动对象(call object).作用域(s ...
- Django笔记-MySQL初次使用设置
以下为个人学习时的笔记,正在完善中........... [1]启动服务 [root@bogon /]# service mysqld start正在启动 mysqld: [确定] [root@bog ...
- C#根据时间产生有序的GUID编码
public static Guid GenerateGuid() { byte[] guidArray = Guid.NewGuid().ToByteArray(); , , ); DateTime ...
- Sphinx 2.2.6 window下安装全过程 未完 持续标记~~~~
由于在win8.1下安装 选的这个版本 Win64 binaries w/MySQL+PgSQL+libstemmer+id64 support 2.2.6-release 7.3M 下载页面 htt ...
- History(历史)命令用法
如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的用法. 使用 HISTTIMEFORMAT 显示时间 ...
- 百万级数据查询到datatable中,提示内存溢出
参考资料: http://group.cnblogs.com/topic/32230.html