大佬教程收集整理的这篇文章主要介绍了cocos2d-x使用tinyxml2解析&存储xml,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我用的是2.1.4的cocos2d-x,里面自带有Tinyxml2库。好像2.1.0以上都有了
导入头文件:
#include "support/Tinyxml2/Tinyxml2.h"
一:创建xml并保存
//要储存XML文件的路径 std::String filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "wociao.xml"; xml文档 XMLDocument *pDoc = new XMLDocument(); if (NULL==pDoC) { return ; } xml声明 XMLDeclaration *pDel = pDoc->NewDeclaration(xml version=\"1.0\" encoding=\"UTF-8\"); pDel) { ; } pDoc->LinkEndChild(pDel); 节点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(StringTextLinkEndChild(StringElement); 节点array XMLElement *arrayElemet = pDoc->NewElement(array); DictElement->LinkEndChild(arrayElemet); for (int i = 0; i<3; i++) { XMLElement *strEle = pDoc->NewElement(); strEle->LinkEndChild(pDoc->NewText(icon)); arrayElemet->LinkEndChild(strElE); } pDoc->SaveFile(filePath.c_str());保存文件 参数:路径 pDoc->Print();打印 delete pDoc;
打印的结果