Cocos2d-x 3.0 parsing XML files with Sax (Chinese display problem resolution)

Source: Internet
Author: User
Tags xml parser

today is a good day. I thought things could become, tomorrow is a good day. Open the Spring Breeze ...

Well, listening to the song, writing a document, life is so enjoyable.

Today, the neighbor of the great God suddenly on the QQ praise me, which makes me extremely excited ah .

It would have to be said a few days ago that it would inadvertently see a cocos on the other. Anyway, it's a lucky draw. That's right. I was in the official T-shirt to go, in line with the greater the size of the larger the more the greater the probability of the Big Lottery, then attached a feeling. And also are more wordy, did not expect the next day Cocos June incredibly give me reply, winning a wood has, COCOS2DX official T-shirt, haha .

。 And then there was the great god neighbor. Today to Cocos Company learning technology when actually saw my long talk about the big, die, is my text better? Ha ha.

Well, then the great God came back and praised me. Then I would like to ask about the summer vacation to their company to stay. In fact, I also want to save time to accumulate a little internship experience. Then, the point came. The big God promised tomorrow to ask personnel to see, maybe summer vacation really has fallen, an instant a big God to take me fly bright ah , while in a good mood. Quickly write two articles to save their own character. hope to have a good news tomorrow !

Well, a person must be optimistic, assuming you do one thing you do not hold hope, then others how to pull, you are just a can't help the pupils! (A . In the near future, more and more pupils, I have to play the game to quit .

。 )


Unconsciously, and pulled so many useless, haha (useless you still pull?) )

I believe that everyone in the use of cocos2d-x more or less will encounter Chinese display problems, to solve the problem is also more diverse, more common has

1. Using Iconv, the engine also provides this library. It's just a Win32 platform, it's going to have to be downloaded on Android. Iconv Library Compilation

2. Write the string into an XML file and parse the XML file in the format according to Strings.xml in Android, which is a better approach, especially if you need to provide international support.

Anyway, I'm more like the other one, why? Because the first kind of useless ~ ~

Well, since I've written about the Android app before, I learned Sax parsing XML. Familiar with this, so introduce this thing.

In simple terms:

Sax is a faster, more efficient approach. It scans the document row by line, parsing while scanning.

The Cocos Engine provides saxparser to parse the XML. I'm not talking nonsense. Let's take a look at the SAXParser class

Class Cc_dll saxparser{    saxdelegator*    _delegator;public:    saxparser ();    ~saxparser (void);    BOOL Init (const char *encoding);     Parse XML    bool Parse (const char* xmlData, size_t datalength);    BOOL Parse (const std::string& filename);               Need to set setdelegator           void Setdelegator (saxdelegator* delegator);                         Parsing method, you need to rewrite the following three methods    //Start a node of    static void startelement (void *ctx, const Cc_xml_char *name, const CC_XML_CHAR * * Atts);    Ends a node    static void EndElement (void *ctx, const cc_xml_char *name);    Text between nodes    static void Texthandler (void *ctx, const cc_xml_char *name, int len);};

Well, we need to set up a delegator. the Delegator class, for example, needs to rewrite the method inside, 3

Class Cc_dll Saxdelegator{public:    virtual ~saxdelegator () {}    virtual void startelement (void *ctx, const char * Name, const char **atts) = 0;    virtual void endElement (void *ctx, const char *name) = 0;    virtual void Texthandler (void *ctx, const char *s, int len) = 0;};

Well. Then we encapsulate a Xmlparser class based on the XML format, for example, I want to read the Strings.xml


XML version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > Small yellow War </string> <string name= "Exit_dialog_title" > Reminders </string> <string name= "Exit_dialog_text" > Are you sure you want to quit? </string> <string name= "Exit_dialog_btn_yes" > OK </string> <string name= "Exit_dialog_ Text_no "> Back </string></resources>

Then implement a Xmlparser class yourself

#pragma once #include <string> #include "cocos2d.h" class Xmlparser:public Cocos2d::ref, public Cocos2d::saxdelega     Tor{public:static xmlparser* parsewithfile (const char *xmlfilename);     Static xmlparser* parsewithstring (const char *content);    Xmlparser (); Virtual ~xmlparser ();//read bool Initwithfile (const char *xmlfilename) from the local XML file;//read from character, can be used to read XML data in the network bool Initwithst Ring (const char *content);//The corresponding XML tag starts, such as: <string name= "app_name" > virtual void startelement (void *ctx, const char     *name, const char **atts); The corresponding XML tag ends, such as:</string> virtual void endElement (void *ctx, const char *name);     corresponding XML label text virtual void Texthandler (void *ctx, const char *s, int len); cocos2d::ccstring* getString (const char *key);    Private:cocos2d::ccdictionary *m_pdictionary;     Std::string M_key;    Std::string startxmlelement; Std::string endxmlelement; };

Detailed implementation:

#include "XMLParser.h" using namespace std;using namespace cocos2d;//character ASCII//space const static INT space = 32;//line-Break Const ST atic int nextline = 10;//tab Transverse tab const static int tab = 9; xmlparser* Xmlparser::p arsewithfile (const char *xmlfilename) {Xmlparser *pxmlparser = new Xmlparser (); if (pxmlparser-   >initwithfile (XMLfileName)) {pxmlparser->autorelease (); return pxmlparser;} Cc_safe_delete (Pxmlparser); return NULL;} BOOL Xmlparser::initwithfile (const char *xmlfilename) {m_pdictionary = new ccdictionary (); SAXParser _parser;_parser.setdelegator (this);//Get file full path string fullPath = Fileutils::getinstance () Fullpathforfilename (XMLfileName); Cclog ("XML parser full path:%s", Fullpath.c_str ()); return _parser.parse (FullPath);} xmlparser* Xmlparser::p arsewithstring (const char *content) {Xmlparser *pxmlparser = new Xmlparser (); if (pxmlparser->   Initwithstring (content)) {pxmlparser->autorelease (); return pxmlparser;} Cc_safe_delete (Pxmlparser); return NULL;} BOOL Xmlparser::initwithstring (constChar *content) {m_pdictionary = new ccdictionary (); SAXParser _parse;_parse.setdelegator (this); return _parse.parse (content, strlen (content));} Start a node//For example <string name= "app_name" > Small yellow Combat </string>//name: String//atts[0] is property: Name//atts[1] is a value: App_n AME//ATTS[2] and so on void xmlparser::startelement (void *ctx, const char *name, const char **atts) {this->startxmlelement = (char *) name; Cclog ("start=%s", Startxmlelement.c_str ()),//nameif (This->startxmlelement = = "string") {while (Atts && * Atts) {cclog ("attrs0=%s", Atts[0]);//atts[0]: Namecclog ("attrs1=%s", Atts[1]);//atts[1]: app_nameconst char *attsKey =    *atts; if (0 = = strcmp (attskey, "name")) {+ + atts;const char *attsvalue = *atts;m_key = Attsvalue;//keybreak;} + + Atts;}}} void Xmlparser::endelement (void *ctx, const char *name) {this->endxmlelement = (char *) name; Cclog ("end=%s", Endxmlelement.c_str ());} void Xmlparser::texthandler (void *ctx, const char *s, int len) {String value ((char *) s, 0, Len);//are all non-normal characters bool NoVaLue = true;for (int i = 0; i < len; ++i) {if (s[i]! = SPACE && s[i]! = nextline && s[i]! = TAB) {Novalue =    False Break;}} if (Novalue) return; String *pstring = string::create (value); Cclog ("key=%s value=%s", M_key.c_str (), pstring->getcstring ()); This->m_pdictionary->setobject (pString, This->m_key);} string* xmlparser::getstring (const char *key) {string strkey (key); return (String *) this->m_pdictionary-> Objectforkey (strkey);} Xmlparser::xmlparser () {}xmlparser::~xmlparser () {cc_safe_delete (this->m_pdictionary);}

and then it's easier to use .

Xmlparser *pxmlparser = xmlparser::p arsewithfile ("Strings.xml"); String *ptitle = pxmlparser->getstring ("Exit_dialog_title");

Well, that's it. Good night!

Cocos2d-x 3.0 parsing XML files with Sax (Chinese display problem resolution)

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: 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.