zoukankan      html  css  js  c++  java
  • 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"}

    ]







  • 相关阅读:
    show proceslist时发现大量的sleep,有什么风险吗,该如何处理?
    监控MySQL的性能,应该主要观察那几个监控项?
    MySQL所有的压力都在一个CPU核心上,为什么会产生这种现象,改如何解决?
    大表,某列无索引,先需要查询该列,删除符合条件的记录,大约占40%数据量,请问有何更好的方案吗?
    MySQL DBA运维中那些动作属于危险性操作?
    云环境上自建MySQL,有哪些高可用实现方案?
    RDS上,MySQL实例中某张表数据小于tmp_table_size,但有查询时会报错临时空间满 The table '/data/mysql/zst/tmp/#sql_13975_23' is full. 原因可能是什么?
    MySQL误删除frm文件该怎么办?
    生产环境MySQL死锁如何监控及如何减少死锁发生的概率。
    MongoDB有哪些优秀特性及适合的场景是什么?
  • 原文地址:https://www.cnblogs.com/shiweihappy/p/4246423.html
Copyright © 2011-2022 走看看