在我们获取网络数据时,基本上使用的都是xml以及json,因此学习对这两种数据进行解析,对我们有很大的帮助。
下面是代码:
HelloParse.h:
<span style="font-size:18px;">#ifndef __HELLOPARSE_H__ #define __HELLOPARSE_H__ #include "cocos2d.h" using namespace cocos2d; class HelloParse : public cocos2d::Layer { public: static cocos2d::Scene* createParseScene(); virtual bool init(); void makejson(); void parsejson(); void makeXML(const char *fileName); void parseXML(const char *fileName); CREATE_FUNC(HelloParse); }; #endif // !__HELLOMAP_H__ </span>
HelloParse.cpp:
#include "HelloParse.h" #include "json/document.h" #include "json/writer.h" #include "json/stringbuffer.h" #include "json/rapidjson.h" #include "tinyxml2/tinyxml2.h" using namespace tinyxml2; using namespace rapidjson; USING_NS_CC; Scene* HelloParse::createParseScene() { auto mapScene = Scene::create(); auto mapLayer = HelloParse::create(); mapScene->addChild(mapLayer); return mapScene; } bool HelloParse::init() { if (!Layer::init()) { return false; } makejson(); parsejson(); makeXML("lake.xml"); parseXML("lake.xml"); return true; } void HelloParse::makejson() { /*生成json */ // 获得一个utf-8编码的文件对象 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()); } void HelloParse::parsejson() { /*解析json */ std::string str = "{\"hello\" : \"word\"}"; CCLOG("%s\n", str.c_str()); rapidjson::Document d; // 将数据以json格式存入到d d.Parse<0>(str.c_str()); if (d.HasParseError()) //打印解析错误 { CCLOG("GetParseError %s\n", d.GetParseError()); } // 获取key为hello的值 if (d.IsObject() && d.HasMember("hello")) { CCLOG("%s\n", d["hello"].GetString());//打印获取hello的值 } } void HelloParse::makeXML(const char *fileName) { // 获取可写入的路径加上文件名 std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName; // 创建一个xml文件 tinyxml2::XMLDocument* pDoc = new tinyxml2::XMLDocument(); //xml 声明(参数可选) XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""); // 添加xml键值对节点结尾 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; } void HelloParse::parseXML(const char *fileName) { std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName; log("%s", filePath.c_str()); tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument(); // 加载指定目录文件,格式正确返回0 XMLError errorId = pDoc->LoadFile(filePath.c_str()); if (errorId != 0) { //xml格式错误 return; } // 获取根节点,即<?xml version="1.0" encoding="UTF-8"?> XMLElement *rootEle = pDoc->RootElement(); //获取第一个节点属性 const XMLAttribute *attribute = rootEle->FirstAttribute(); //打印节点属性名和值 log("attribute_name = %s,attribute_value = %s", attribute->Name(), attribute->Value()); // 根据key获取值 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; }
运行结果:
创建json:
{"json":"json string","array":[{"int":1,"double":1.0,"bool":true,"hello":"???"}]}
{"hello" : "word"}
解析json:
word
创建xml:
<?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>
解析xml:
C:/Users/Administrator/AppData/Local/Helloworld/lake.xml
attribute_name = version,attribute_value = 1.0
keyEle Text= Text
childEle Text= Cocos2d-x
childEle Text= Cocos2d-x
childEle Text= Cocos2d-x
版权声明:转载请标明出处。
时间: 2024-10-11 21:56:51