cocos2d-x使用tinyxml2存储解析xml

我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库。

导入头文件:
#include "support/tinyxml2/tinyxml2.h"

using namespace tinyxml2;

一:创建xml并保存

void TinyXmlDemo::createTinyXMLFile()
{
//储存XML文件的路径
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "tinyxmltest.xml";
//xml文档
tinyxml2::XMLDocument* pDoc = new tinyxml2::XMLDocument();
if (pDoc == NULL)
{
return;
}
//xml声明
XMLDeclaration* pDecl = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
if (pDecl == NULL)
{
return;
}
pDoc->LinkEndChild(pDecl);
//节点plist
XMLElement* plistElement = pDoc->NewElement("plist");
plistElement->SetAttribute("version","1.0");
pDoc->LinkEndChild(plistElement); //节点dict
XMLElement* dictElement = pDoc->NewElement("dict");
plistElement->LinkEndChild(dictElement);
//节点key
XMLElement* keyElement = pDoc->NewElement("key");
keyElement->LinkEndChild(pDoc->NewText("keyText"));//给节点设置值
dictElement->LinkEndChild(keyElement);
//节点string
XMLElement* stringElement = pDoc->NewElement("string");
stringElement->LinkEndChild(pDoc->NewText("stringText"));//给节点设置值
dictElement->LinkEndChild(stringElement);
//节点array
XMLElement* arrayElemet = pDoc->NewElement("array");
dictElement->LinkEndChild(arrayElemet);
for (int i = ; i<; i++)
{
XMLElement* strEle = pDoc->NewElement("string");
strEle->LinkEndChild(pDoc->NewText("icon"));
arrayElemet->LinkEndChild(strEle);
}
//保存文件
pDoc->SaveFile(filePath.c_str());
delete pDoc; }

在win32上xml保存的路径是在D:\cocos2d-x-2.1.4\cocos2d-x-2.1.4\Debug.win32里面

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>keyText</key>
<string>stringText</string>
<array>
<string>icon</string>
<string>icon</string>
<string>icon</string>
</array>
</dict>
</plist>

二:解析xml

void TinyXmlDemo::parseTinyXMLFile()
{
//储存XML文件的路径
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "tinyxmltest.xml";
tinyxml2::XMLDocument* pDoc = new tinyxml2::XMLDocument();
pDoc->LoadFile(filePath.c_str());
//得到根节点
XMLElement* rootElement = pDoc->RootElement();
//打印根节点名字和值
CCLog("%s,%s",rootElement->Value(),rootElement->GetText());
//节点的第一个属性
const XMLAttribute* attribute = rootElement->FirstAttribute();
//打印属性的名字和值
CCLog("%s %s",attribute->Name(),attribute->Value());
//查找节点的属性值
float value = 0.1f;
rootElement->QueryFloatAttribute("version",&value);
CCLog("%f",value);
//设置节点属性值
rootElement->SetAttribute("version", 1.4);
//跟节点的第一个字节点 dict
XMLElement *dictEle = rootElement->FirstChildElement();
//dict下面的子节点 key
XMLElement *keyEle = dictEle->FirstChildElement();
//打印key节点的值
CCLog("%s,%s",keyEle->Name(),keyEle->GetText());
//key节点的next节点 string
XMLElement *stringEle = keyEle->NextSiblingElement();
CCLog("%s,%s",stringEle->Name(),stringEle->GetText());
//string节点的子节点
XMLElement *nulXmlEle = stringEle->FirstChildElement();
if (NULL == nulXmlEle) {
CCLog("string no childElement");
}
//array节点
XMLElement *arrayEle= stringEle->NextSiblingElement();
CCLog("%s,%s",arrayEle->Name(),arrayEle->GetText());
XMLElement *array1 = arrayEle->FirstChildElement();
CCLog("%s,%s",array1->Name(),array1->GetText());
XMLElement *array2 = array1->NextSiblingElement();
CCLog("%s,%s",array2->Name(),array2->GetText());
XMLElement *array3 = arrayEle->LastChildElement();
CCLog("%s,%s",array3->Name(),array3->GetText()); //修改了xml记得保存
pDoc->SaveFile(filePath.c_str());
}

打印输出:

plist,(null)
version 1.0
1.000000
key,keyText
string,stringText
string no childElement
array,(null)
string,icon
string,icon
string,icon

由于修改了节点属性值,保存后的xml

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.4">
<dict>
<key>keyText</key>
<string>stringText</string>
<array>
<string>icon</string>
<string>icon</string>
<string>icon</string>
</array>
</dict>
</plist>

参考http://blog.csdn.net/w18767104183/article/details/19837203

上一篇:转载:js 创建对象、属性、方法


下一篇:servlet之session设置