大佬教程收集整理的这篇文章主要介绍了cocos2d-x 读写 xml 文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
static void makeXml(const char *fileName) { std::string filePath = FileUtils::geTinstance()->getWritablePath() + filename; Tinyxml2::XMLDocument *pDoc = new Tinyxml2::XMLDocument(); //xml 声明(参数可选) XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""); pDoc->LinkEndChild(pDel); //添加plist节点 XMLElement *plistElement = pDoc->NewElement("plist"); plistElement->SetAttribute("version","1.0"); pDoc->LinkEndChild(plistElement); XMLComment *commentElement = pDoc->NewComment("this is xml comment"); plistElement->LinkEndChild(commentElement); //添加Dic节点 XMLElement *DicElement = pDoc->NewElement("Dic"); plistElement->LinkEndChild(DicElement); //添加key节点 XMLElement *keyElement = pDoc->NewElement("key"); keyElement->LinkEndChild(pDoc->NewText("Text")); DicElement->LinkEndChild(keyElement); XMLElement *arrayElement = pDoc->NewElement("array"); DicElement->LinkEndChild(arrayElement); for (int i = 0; i<3; i++) { XMLElement *elm = pDoc->NewElement("name"); elm->LinkEndChild(pDoc->NewText("Cocos2d-x")); arrayElement->LinkEndChild(elm); } pDoc->SaveFile(filePath.c_str()); pDoc->Print(); delete pDoc; } static void parseXML(const char *fileName) { std::string filePath = FileUtils::geTinstance()->getWritablePath() + filename; Tinyxml2::XMLDocument *pDoc = new Tinyxml2::XMLDocument(); XMLError errorId = pDoc->LoadFile(filePath.c_str()); if (errorId != 0) { //xml格式错误 return; } XMLElement *rootEle = pDoc->RootElement(); //获取第一个节点属性 const XMLAttribute *attribute = rootEle->FirstAttribute(); //打印节点属性名和值 log("attribute<em>name = %s,attribute</em>value = %s",attribute->Name(),attribute->Value()); XMLElement *DicEle = rootEle->FirstChildElement("Dic"); XMLElement *keyEle = DicEle->FirstChildElement("key"); if (keyElE) { log("keyEle Text= %s",keyEle->GetText()); } XMLElement *arrayEle = keyEle->NextSiblingElement(); XMLElement *childEle = arrayEle->FirstChildElement(); while ( childEle ) { log("childEle Text= %s",childEle->GetText()); childEle = childEle->NextSiblingElement(); } delete pDoc; }
以上是大佬教程为你收集整理的cocos2d-x 读写 xml 文件全部内容,希望文章能够帮你解决cocos2d-x 读写 xml 文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。