读取和生成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"}
]