Cocos2d-x in XML analysis and data storage

Source: Internet
Author: User

Inadvertently play the game of the week, hey, time is always playing so fast when.

。。

So today I decided to look at XML that was not very familiar to me before, and the data was stored in XML before the game. But this one is still a colleague doing, so not very familiar with),

Looked at the XML and parsing methods he wrote, and then improved it himself, so come up with a simple summary and share

The main concerns are:

1. XML creation

Analysis of 2.xml

3. Save the parsed XML data with a vector;


For example, the following: (after writing the XML, the simplest check syntax error is to open with IE browser to see, can open the syntax is correct)

<?xml version= "1.0" encoding= "Utf-8"?

><mineral><mineral><type>1</type><times>100</times><p>20</p ></mineral><mineral><type>4</type><times>100</times><p>20</p ></mineral><mineral><type>5</type><times>100</times><p>20</p ></mineral></Mineral>

Here I created a new XML named "Mineral.xml" in the form of an online XML writing format.

(mineral is the meaning of the mine, I have arbitrarily written 3 types of ore in XML.) Each ore has its own type, magnification, probability)

Then save it in the resource directory and create a new Cocos2d-x project,

The following post the main parsing code

. h file

#ifndef __helloworld_scene_h__#define __helloworld_scene_h__#include "cocos2d.h" #include <string> #include <vector>typedef struct mineral{int times; int type;int p;} *ptrmineral;class helloworld:public cocos2d::cclayer{public:    virtual bool init ();      Static cocos2d::ccscene* scene ();    void Menuclosecallback (ccobject* psender);    BOOL Readminearlxml (); void Displayvec ();    



. cpp Files

#include "HelloWorldScene.h" #include ". /support/tinyxml2/tinyxml2.h "using namespace TINYXML2; 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;  }readminearlxml ();    Displayvec (); return true;} BOOL Helloworld::readminearlxml () {tinyxml2::xmldocument* xmlData = new tinyxml2::xmldocument (); unsigned long nSize; const char *pxmlbuffer = (const char*) ccfileutils::sharedfileutils ()->getfiledata ("Xml/mineral.xml", "RB",& NSize); if (NULL = = Pxmlbuffer) {cclog ("read Mineral.xml Failed");} Else Cclog ("star read Mineral.xml"); Xmldata->parse (pxmlbuffer,nsize); XMLElement *rootnode = Xmldata->rootelement (); if (!rootnode) {return false;} xmlelement* Curnode = rootnode->firstchildelement ("mineral"); while (null!= Curnode) {ptrmineral pmineral =new Mineral ();p Mineral->type = (atoi) (CurnOde->firstchildelement ("type"))->gettext ());p mineral->times = (atoi) ((Curnode->firstchildelement (" Times "))->gettext ());p mineral->p = (atoi) ((Curnode->firstchildelement (" P "))->gettext ()); m_ Pmineralvec.push_back (pmineral); curnode = Curnode->nextsiblingelement ("mineral");} Delete Xmldata;return true;} void HelloWorld::d Isplayvec () {Cclog ("*********m_pmineralvec*********"); for (int i = 0; I<m_pmineralvec.size (); i++ ) {Cclog ("<mineral>"); Cclog ("<type> =%i </type>", M_pmineralvec[i]->type); Cclog ("<times> =%i </times>", m_pmineralvec[i]->times); Cclog ("<p> =%i </p>", m_pmineralvec[i]->p); Cclog ("</mineral>");}} 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} 

The above includes XML parsing, XML data display, respectively:

Show demo samples such as the following:

Star Read mineral.xml*********m_pmineralvec*********<mineral><type> = 1 </type><times> = 100 </times><p>     = </p></mineral><mineral><type> = 4 </type><times > = + </times><p>     = </p></mineral><mineral><type> = 5 </type> <times> = </times><p>     = </p></mineral>

Control the program. Output and previously created XML consistency, so that the XML at the resolution OK that, it is not very easy AH

Cocos2d-x in XML analysis and data storage

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.