• cocos2d-x 3.2读取xml和json练习


    读取和生成xml文件:
    #include "tinyxml2/tinyxml2.h"
    using namespace tinyxml2;
    
    
    void HelloWorld::makeXml(const char* fileName)
    {
        //写入路径
        std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
        XMLDocument *pDoc = new XMLDocument();
        //头声明
        XMLDeclaration *pDecl = pDoc->NewDeclaration("xml=version="1.0" encoding="UTF-8"");
        pDoc->LinkEndChild(pDecl);
        //注释
        XMLComment *pCom = pDoc->NewComment("test xml");
        pDoc->LinkEndChild(pCom);
        
        XMLElement *plistEl = pDoc->NewElement("plist");
        plistEl->SetAttribute("version", "1.0");
        plistEl->SetAttribute("age", 10);
        pDoc->LinkEndChild(plistEl);
        
        XMLElement *dictEl = pDoc->NewElement("dict");
        plistEl->LinkEndChild(dictEl);
        
        XMLElement *keyEl = pDoc->NewElement("key");
        keyEl->LinkEndChild(pDoc->NewText("keyValue"));
        dictEl->LinkEndChild(keyEl);
        
        XMLElement *arrayEl = pDoc->NewElement("array");
        dictEl->LinkEndChild(arrayEl);
        for (int i = 0; i<2 ; i++)
        {
            XMLElement *nameEl = pDoc->NewElement("name");
            nameEl->LinkEndChild(pDoc->NewText("array value"));
            arrayEl->LinkEndChild(nameEl);
        }
        
        pDoc->SaveFile(filePath.c_str());
        pDoc->Print();
        delete pDoc;
    }
    
    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)
        {
            return;
        }
        
        XMLElement *rootEl = pDoc->RootElement();
        const XMLAttribute *attribute = rootEl->FirstAttribute();
        while (attribute)
        {
            CCLOG("name=%s, value = %s", attribute->Name(), attribute->Value());
            attribute = attribute->Next();
        }
        
        XMLElement *dictEl = rootEl->FirstChildElement("dict");
        XMLElement *keyEl = dictEl->FirstChildElement("key");
        if (keyEl)
        {
            CCLOG("key el is = %s", keyEl->GetText());
        }
        
        XMLElement *arrayEl = keyEl->NextSiblingElement();
        XMLElement *childEl = arrayEl->FirstChildElement();
        while (childEl)
        {
            CCLOG("child el is = %s", childEl->GetText());
            childEl = childEl->NextSiblingElement();
        }
        
    }
    
    

    读取和生成json

    #include "json/rapidjson.h"
    #include "json/document.h"
    #include "json/writer.h"
    #include "json/stringbuffer.h"

    例子:

    void ReadAndWriteJsonScene::readJson()
    {
        std::string name = "testJson.json";
        rapidjson::Document doc;
        if (!FileUtils::getInstance()->isFileExist(name))
        {
            CCLOG("file is not exist");
            return;
        }
        
        std::string data = FileUtils::getInstance()->getStringFromFile(name);
        doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());
        if (doc.HasParseError() || !doc.IsArray())
        {
            return;
        }
        
        for (auto i = 0; i<doc.Size(); i++)
        {
            rapidjson::Value &v = doc[i];
            std::string name;
            int age;
            std::string sex;
            if (v.HasMember("name"))
            {
                name = v["name"].GetString();
                auto len = v["name"].GetStringLength();
                CCLOG("name is %s, len is %d", name.c_str(), len);
            }
            
        }
        
    }
    
    void ReadAndWriteJsonScene::writeJson()
    {
        rapidjson::Document doc;
        doc.SetObject();
        rapidjson::Document::AllocatorType &allocator = doc.GetAllocator();
        rapidjson::Value arr(rapidjson::kArrayType);
        rapidjson::Value obj(rapidjson::kObjectType);
        obj.AddMember("int", 1, allocator);
        obj.AddMember("double", 2.0, allocator);
        obj.AddMember("bool", true, allocator);
        obj.AddMember("hello", "xxxxx", allocator);
        arr.PushBack(obj, allocator);
        
        doc.AddMember("strX", "jsonTest", allocator);
        doc.AddMember("arr", arr, allocator);
        
        rapidjson::StringBuffer buffer;
        rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
        doc.Accept(writer);
        
        CCLOG("buffer string = %s", buffer.GetString());
    }
    


    测试的json文件

    [

      {"name":"cl","age":27,"sex":"M"},

      {"name":"cbsss","age":25,"sex":"W"},

      {"name":"gx","age":25,"sex":"M"},

      {"name":"hxl","age":27,"sex":"W"}

    ]







  • 相关阅读:
    围棋角部大眼的气
    openwrt 增加自定义包
    OpenWrt – 编译前的./scripts/feeds脚本用法
    memset 赋初值为无穷大和无穷小
    android 自动点击学习课程
    最多硬币问题
    最少硬币问题
    9.2 嵌套矩形问题
    ajax请求到一个controller在跳转到下载的controller上也不能下载,百度了一下总结下原因:发现原来jQuery的ajax回调已经把response的数据傻瓜式的以字符串的方式解析.
    MySQL 数据库的远程访问权限
  • 原文地址:https://www.cnblogs.com/shiweihappy/p/4246423.html
Copyright © 2020-2023  润新知