cocos2d-x使用libjson和tinyxml解析json和xml的代码示例

前端之家收集整理的这篇文章主要介绍了cocos2d-x使用libjson和tinyxml解析json和xml的代码示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件helloword.h

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3. #include "cocos2d.h"
  4. #include "cocos-ext.h"
  5. #include "libjson.h"
  6. #include "tinyxml.h"
  7. USING_NS_CC_EXT;
  8. USING_NS_CC;
  9. class HelloWorld : public cocos2d::CCLayer
  10. {
  11. public:
  12. virtual bool init();
  13. static cocos2d::CCScene* scene();
  14. void menuCloseCallback(CCObject* pSender);
  15. CREATE_FUNC(HelloWorld);
  16. void json();
  17. void jsonparse(JSONNode &n);
  18. void xml();
  19. };
  20.  
  21. #endif // __HELLOWORLD_SCENE_H__


helloword.cpp的内容,使用两者的方法

  1. #include "HelloWorldScene.h"
  2. #include "variate.h"
  3. #include "cocos-ext.h"
  4. #include <fstream>
  5. using namespace std;
  6. USING_NS_CC_EXT;
  7. USING_NS_CC;
  8. CCScene* HelloWorld::scene()
  9. {
  10. CCScene *scene = CCScene::create();
  11. HelloWorld *layer = HelloWorld::create();
  12. scene->addChild(layer);
  13. return scene;
  14. }
  15. bool HelloWorld::init()
  16. {
  17. if ( !CCLayer::init() )
  18. {
  19. return false;
  20. }
  21. CCMenuItemFont *font=CCMenuItemFont::create("json");
  22. font->setFontSize(30);
  23. font->setAnchorPoint(ccp(0,0));
  24. font->setPosition(ccp(40,8));
  25. CCMenuItemFont *font2=CCMenuItemFont::create("xml");
  26. font2->setAnchorPoint(ccp(0,0));
  27. font2->setPosition(ccp(40,8));
  28. CCMenuItemImage *image1=CCMenuItemImage::create("btn-test-0.png","btn-test-0.png",this,menu_selector(HelloWorld::json));
  29. image1->addChild(font);
  30. CCMenuItemImage *image2=CCMenuItemImage::create("btn-test-0.png",menu_selector(HelloWorld::xml));
  31. image2->addChild(font2);
  32. CCMenu *menu=CCMenu::create(image1,image2,NULL);
  33. menu->alignItemsVertically();//menu->alignItemsHorizontally();
  34. menu->setPosition(Visibel_mid.x,Visibel_mid.y-100);
  35. addChild(menu);
  36. return true;
  37. }
  38. void HelloWorld::json()
  39. {
  40. JSONNode n(JSON_NODE);
  41. n.push_back(JSONNode("child","this is a child json"));
  42. JSONNode n1(JSON_NODE);
  43. JSONNode n2(JSON_NODE);
  44. n1.push_back(JSONNode("child a","this is child a"));
  45. n1.push_back(JSONNode("child b","this is child b"));
  46. n2.push_back(JSONNode("child c","this is child c"));
  47. n2.push_back(JSONNode("child d","this is child d"));
  48. n2.push_back(JSONNode("child e","this is child e"));
  49. JSONNode array(JSON_ARRAY);
  50. array.set_name("name");
  51. array.push_back(n1);
  52. array.push_back(n2);
  53. n.push_back(array);
  54. cout<<n.write_formatted();
  55. //数据化的持久化存储
  56. string datapath=CCFileUtils::sharedFileUtils()->getWritablePath()+"info.text";
  57. fstream files;
  58. files.open(datapath.c_str(),ios::out);
  59. files<<n.write_formatted();
  60. files.close();
  61. CCLOG("%s",datapath.c_str());
  62. //读取文章内容
  63. CCString *contentstr=CCString::createWithContentsOfFile(datapath.c_str());
  64. JSONNode jsonnode=libjson::parse(contentstr->getCString());
  65. this->jsonparse(jsonnode);
  66. }
  67. void HelloWorld::jsonparse(JSONNode &n)
  68. {
  69. JSONNode::json_iterator itror=n.begin();
  70. while (itror!=n.end()) {
  71. if (itror->type()!=JSON_NODE||itror->type()!=JSON_ARRAY) {
  72. jsonparse(*itror);
  73. }
  74. if (itror->name()=="child") {
  75. CCLOG("%s",itror->as_string().c_str());
  76. }
  77. if (itror->name()=="child a") {
  78. CCLOG("%s",itror->as_string().c_str());
  79. }
  80. if (itror->name()=="child b") {
  81. CCLOG("%s",itror->as_string().c_str());
  82. }
  83. if (itror->name()=="child c") {
  84. CCLOG("%s",itror->as_string().c_str());
  85. }
  86. if (itror->name()=="child d") {
  87. CCLOG("%s",itror->as_string().c_str());
  88. }
  89. if (itror->name()=="child e") {
  90. CCLOG("%s",itror->as_string().c_str());
  91. }
  92. itror++;
  93. }
  94. }
  95. void HelloWorld::xml()
  96. {
  97. //源文件
  98. string bundle = CCFileUtils::sharedFileUtils()->fullPathForFilename("news.xml");
  99. //获取文件内容
  100. CCString *stringpp=CCString::createWithContentsOfFile(bundle.c_str());
  101. //获取沙盒地址
  102. string xmlpath=CCFileUtils::sharedFileUtils()->getWritablePath()+"new.xml";
  103. CCLog("%s",xmlpath.c_str());
  104. fstream file;
  105. file.open(xmlpath.c_str(),ios::out);
  106. file<<stringpp->getCString();
  107. file.close();
  108. TiXmlDocument *document=new TiXmlDocument(xmlpath.c_str());
  109. document->LoadFile();//加载文件
  110. TiXmlElement *rootelement=document->RootElement();
  111. cout<<rootelement->Value()<<endl;
  112. //一级一级获取
  113. TiXmlElement *channel=rootelement->FirstChildElement();
  114. TiXmlElement *title=channel->FirstChildElement();
  115. cout<<title->Value()<<endl;
  116. //获取兄弟节点
  117. TiXmlElement *link=title->NextSiblingElement("link");
  118. // TiXmlElement *link=title->NextSiblingElement()->NextSiblingElement()
  119. // ->NextSiblingElement();
  120. cout<<link->GetText()<<endl;
  121. //增加
  122. TiXmlElement *newelement=new TiXmlElement("channel");
  123. newelement->SetAttribute("name","123");
  124. TiXmlText text("uuuuuuuuuuuuuuu");
  125. newelement->LinkEndChild(&text);
  126. //rootelement->InsertEndChild(*newelement);//加到末尾
  127. rootelement->InsertBeforeChild(rootelement->FirstChildElement(),*newelement);
  128. document->SaveFile();
  129. //替换
  130. TiXmlElement *newelement1=new TiXmlElement("csdf");
  131. newelement1->SetAttribute("ndf","sdf3");
  132. TiXmlText text1("dsfsuuuu");
  133. newelement1->LinkEndChild(&text1);
  134. rootelement->ReplaceChild(rootelement->FirstChildElement(),*newelement1);
  135. document->SaveFile();
  136. //删除
  137. // rootelement->RemoveChild(rootelement->FirstChildElement()->NextSiblingElement());
  138. // document->SaveFile();
  139. TiXmlElement *two=new TiXmlElement("title");
  140. //TiXmlText text2("yyyy");
  141. two->LinkEndChild(newelement);
  142. rootelement->InsertEndChild(*two);
  143. document->SaveFile();
  144. }
  145. void HelloWorld::menuCloseCallback(CCObject* pSender)
  146. {
  147. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  148. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  149. #else
  150. CCDirector::sharedDirector()->end();
  151. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  152. exit(0);
  153. #endif
  154. #endif
  155. }

猜你在找的Cocos2d-x相关文章