头文件helloword.h
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- #include "cocos-ext.h"
- #include "libjson.h"
- #include "tinyxml.h"
- USING_NS_CC_EXT;
- USING_NS_CC;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- void menuCloseCallback(CCObject* pSender);
- CREATE_FUNC(HelloWorld);
- void json();
- void jsonparse(JSONNode &n);
- void xml();
- };
- #endif // __HELLOWORLD_SCENE_H__
- #include "HelloWorldScene.h"
- #include "variate.h"
- #include "cocos-ext.h"
- #include <fstream>
- using namespace std;
- USING_NS_CC_EXT;
- USING_NS_CC;
- CCScene* HelloWorld::scene()
- {
- CCScene *scene = CCScene::create();
- HelloWorld *layer = HelloWorld::create();
- scene->addChild(layer);
- return scene;
- }
- bool HelloWorld::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCMenuItemFont *font=CCMenuItemFont::create("json");
- font->setFontSize(30);
- font->setAnchorPoint(ccp(0,0));
- font->setPosition(ccp(40,8));
- CCMenuItemFont *font2=CCMenuItemFont::create("xml");
- font2->setAnchorPoint(ccp(0,0));
- font2->setPosition(ccp(40,8));
- CCMenuItemImage *image1=CCMenuItemImage::create("btn-test-0.png","btn-test-0.png",this,menu_selector(HelloWorld::json));
- image1->addChild(font);
- CCMenuItemImage *image2=CCMenuItemImage::create("btn-test-0.png",menu_selector(HelloWorld::xml));
- image2->addChild(font2);
- CCMenu *menu=CCMenu::create(image1,image2,NULL);
- menu->alignItemsVertically();//menu->alignItemsHorizontally();
- menu->setPosition(Visibel_mid.x,Visibel_mid.y-100);
- addChild(menu);
- return true;
- }
- void HelloWorld::json()
- {
- JSONNode n(JSON_NODE);
- n.push_back(JSONNode("child","this is a child json"));
- JSONNode n1(JSON_NODE);
- JSONNode n2(JSON_NODE);
- n1.push_back(JSONNode("child a","this is child a"));
- n1.push_back(JSONNode("child b","this is child b"));
- n2.push_back(JSONNode("child c","this is child c"));
- n2.push_back(JSONNode("child d","this is child d"));
- n2.push_back(JSONNode("child e","this is child e"));
- JSONNode array(JSON_ARRAY);
- array.set_name("name");
- array.push_back(n1);
- array.push_back(n2);
- n.push_back(array);
- cout<<n.write_formatted();
- //数据化的持久化存储
- string datapath=CCFileUtils::sharedFileUtils()->getWritablePath()+"info.text";
- fstream files;
- files.open(datapath.c_str(),ios::out);
- files<<n.write_formatted();
- files.close();
- CCLOG("%s",datapath.c_str());
- //读取文章内容
- CCString *contentstr=CCString::createWithContentsOfFile(datapath.c_str());
- JSONNode jsonnode=libjson::parse(contentstr->getCString());
- this->jsonparse(jsonnode);
- }
- void HelloWorld::jsonparse(JSONNode &n)
- {
- JSONNode::json_iterator itror=n.begin();
- while (itror!=n.end()) {
- if (itror->type()!=JSON_NODE||itror->type()!=JSON_ARRAY) {
- jsonparse(*itror);
- }
- if (itror->name()=="child") {
- CCLOG("%s",itror->as_string().c_str());
- }
- if (itror->name()=="child a") {
- CCLOG("%s",itror->as_string().c_str());
- }
- if (itror->name()=="child b") {
- CCLOG("%s",itror->as_string().c_str());
- }
- if (itror->name()=="child c") {
- CCLOG("%s",itror->as_string().c_str());
- }
- if (itror->name()=="child d") {
- CCLOG("%s",itror->as_string().c_str());
- }
- if (itror->name()=="child e") {
- CCLOG("%s",itror->as_string().c_str());
- }
- itror++;
- }
- }
- void HelloWorld::xml()
- {
- //源文件
- string bundle = CCFileUtils::sharedFileUtils()->fullPathForFilename("news.xml");
- //获取文件内容
- CCString *stringpp=CCString::createWithContentsOfFile(bundle.c_str());
- //获取沙盒地址
- string xmlpath=CCFileUtils::sharedFileUtils()->getWritablePath()+"new.xml";
- CCLog("%s",xmlpath.c_str());
- fstream file;
- file.open(xmlpath.c_str(),ios::out);
- file<<stringpp->getCString();
- file.close();
- TiXmlDocument *document=new TiXmlDocument(xmlpath.c_str());
- document->LoadFile();//加载文件
- TiXmlElement *rootelement=document->RootElement();
- cout<<rootelement->Value()<<endl;
- //一级一级获取
- TiXmlElement *channel=rootelement->FirstChildElement();
- TiXmlElement *title=channel->FirstChildElement();
- cout<<title->Value()<<endl;
- //获取兄弟节点
- TiXmlElement *link=title->NextSiblingElement("link");
- // TiXmlElement *link=title->NextSiblingElement()->NextSiblingElement()
- // ->NextSiblingElement();
- cout<<link->GetText()<<endl;
- //增加
- TiXmlElement *newelement=new TiXmlElement("channel");
- newelement->SetAttribute("name","123");
- TiXmlText text("uuuuuuuuuuuuuuu");
- newelement->LinkEndChild(&text);
- //rootelement->InsertEndChild(*newelement);//加到末尾
- rootelement->InsertBeforeChild(rootelement->FirstChildElement(),*newelement);
- document->SaveFile();
- //替换
- TiXmlElement *newelement1=new TiXmlElement("csdf");
- newelement1->SetAttribute("ndf","sdf3");
- TiXmlText text1("dsfsuuuu");
- newelement1->LinkEndChild(&text1);
- rootelement->ReplaceChild(rootelement->FirstChildElement(),*newelement1);
- document->SaveFile();
- //删除
- // rootelement->RemoveChild(rootelement->FirstChildElement()->NextSiblingElement());
- // document->SaveFile();
- TiXmlElement *two=new TiXmlElement("title");
- //TiXmlText text2("yyyy");
- two->LinkEndChild(newelement);
- rootelement->InsertEndChild(*two);
- document->SaveFile();
- }
- void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
- CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
- #else
- CCDirector::sharedDirector()->end();
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- #endif
- }