Cocos2d-x code examples that parse JSON and XML using Libjson and TinyXML __js

Source: Internet
Author: User

Header file 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__


Helloword.cpp content, using both methods

#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", "btn-test-0.png", this, 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", "It is a child json"));
    Jsonnode N1 (Json_node);
    Jsonnode n2 (Json_node);
    N1.push_back (Jsonnode ("Child a", "It is Child a"));
    N1.push_back (Jsonnode ("Child B", "It is Child B"));
    N2.push_back (Jsonnode ("Child C", "It is Child C"));
    N2.push_back (Jsonnode ("Child D", "It is Child d"));
    N2.push_back (Jsonnode ("Child e", "It 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 ();
    Data-persisted storage 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 ());
    Read the content of the article Ccstring *contentstr=ccstring::createwithcontentsoffile (Datapath.c_str ());
    Jsonnode Jsonnode=libjson::p arse (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 () {//source file String bundle = Ccfileutils::sharedfileutils ()->fullpathforfilename ("New
        S.xml ");
    Gets the contents of the file Ccstring *stringpp=ccstring::createwithcontentsoffile (Bundle.c_str ());
    Gets the sandbox address 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 ()//Loading file tixmlelement *rootelement=document->rootelement ();
        Cout<<rootelement->value () <<endl; First-level access to tixmlelement *channel=rootelement->firstchildelement ();
    Tixmlelement *title=channel->firstchildelement ();
        Cout<<title->value () <<endl;
Get sibling node tixmlelement *link=title->nextsiblingelement ("link");
    Tixmlelement *link=title->nextsiblingelement ()->nextsiblingelement ()//->nextsiblingelement ();
    Cout<<link->gettext () <<endl;
    Increased tixmlelement *newelement=new tixmlelement ("channel");
    Newelement->setattribute ("name", "123");
    Tixmltext text ("Uuuuuuuuuuuuuuu");
    Newelement->linkendchild (&text); Rootelement->insertendchild (*newelement)//Add to end Rootelement->insertbeforechild (rootelement->
    Firstchildelement (), *newelement);
    Document->savefile ();
    Replace Tixmlelement *newelement1=new tixmlelement ("Csdf");
    Newelement1->setattribute ("NDF", "sdf3");
    Tixmltext Text1 ("dsfsuuuu");
    Newelement1->linkendchild (&AMP;TEXT1);
   Rootelement->replacechild (Rootelement->firstchildelement (), *newelement1); Document->savefile ();
Delete//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 don't implement a close button. ", Alert");
#else ccdirector::shareddirector ()->end ();
#if (Cc_target_platform = = Cc_platform_ios) exit (0);
 #endif #endif}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.