Cocos2d-x Read and write XML files

Source: Internet
Author: User

Cocos2d-x Read and write XML files

A Product of Cheungmine

the use of cocos2d-x to develop 2d games is really convenient, but for the general small game, often need to work is UI layout design and adjustment, code changes to change, really inconvenient. Do the game development sooner or later to have their own UI module. It may be called cocos2d-layout. Cocos2d-layout corresponds to the scene of the stage. The scene master builds the stage according to the director's requirements (XML). The scene master in real life is of course a person to do, in the program is a program or code base. This useless universal library can do this thing, because the content of the game is very different. So a good game developer's Toolbox must have such a Swiss army knife. I am just getting started, start with the simplest code.

Using XML to do such a configuration is most appropriate. Cocos2d-x provides a C + + library tinyxml2 (HTTPS://GITHUB.COM/LEETHOMASON/TINYXML2)that reads and writes XML, and of course it is open source, which is fairly simple to use:

#include "Tinyxml2/tinyxml2.h"

using namespace TINYXML2;

Put the following code in your cocos2d code and call it:

static void Makexml (const char *filename) {std::string FilePath = fileutils::getinstance ()->getwritablepath () + file    Name;    Tinyxml2::xmldocument *pdoc = new Tinyxml2::xmldocument ();    XML declaration (optional parameter) XmlDeclaration *pdel = pdoc->newdeclaration ("xml version=\" 1.0\ "encoding=\" utf-8\ "");    Pdoc->linkendchild (Pdel);    Add plist node XMLElement *plistelement = Pdoc->newelement ("plist");    Plistelement->setattribute ("Version", "1.0");    Pdoc->linkendchild (plistelement);    Xmlcomment *commentelement = Pdoc->newcomment ("This is XML comment");    Plistelement->linkendchild (commentelement);    Add dic node XMLElement *dicelement = Pdoc->newelement ("dic");    Plistelement->linkendchild (dicelement);    Add key node 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 () + fil    ename;    Tinyxml2::xmldocument *pdoc = new Tinyxml2::xmldocument ();    Xmlerror ErrorID = Pdoc->loadfile (Filepath.c_str ());    if (ErrorID! = 0) {//xml format error return;    } XMLElement *rootele = Pdoc->rootelement ();    Gets the first node property of const XmlAttribute *attribute = Rootele->firstattribute (); Print node property name and value 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;}

Of course, the above code is searched online. Just an example. If you want to use C to parse XML, there are many choices: Libxml,expat, Ezxml, and so on. I found a very good library: Mini-xml. Http://www.msweet.org/projects.php?Z3
Mini-xml is a small XML library that's can use to read and write XML and xml-like data files in your application without requiring large non-standard libraries. Mini-xml only requires an ANSI C compatible compiler (GCC works, as does most vendors ' ANSI C compilers) and a ' make ' progra M.mini-xml supports reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files and strings. Data is stored in a linked-list tree structure, preserving the XML data hierarchy, and arbitrary element names, attributes , and attribute values is supported with no  preset limits, just available memory.

So I'm going to do it with this.Cocos2d-layout。


Cocos2d-x Read and write XML files

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.