Cocos2d-x 3.0 uses Sax to parse xml documents (solving Chinese display problems)

Source: Internet
Author: User

Today is a good day. Everything you want can be done. Tomorrow is a good day....

Well, it's so enjoyable to listen to songs and write documents.

Today, my neighbors suddenly liked me on qq,This made me very excited... This should be mentioned a few days ago. It would have inadvertently seen a real-name activity on cocos. It was just like taking part in a lucky draw. That's right,I went with the official T-shirt and adhered to the principle that the larger the numerator, the larger the denominator, and the higher the chance of a lucky draw,Then I sent a sigh of relief, and it was quite cool,I didn't expect cocos to reply to me the next day. I won the prize and got an official cocos2dx T-shirt. Haha.. Then, when I went to cocos to study technology, my old great neighbor saw my long talk, and I was dying. Is it better for me? Haha .. Okay, then the great god came back to give me a thumbs up, and I asked by the way if I could stay in their company during the summer vacation. In fact, I also wanted to accumulate some practical experience while I had time. Then, the point is. The great God promised to ask personnel tomorrow to see if the summer vacation might have fallen,In an instant, there is a kind of great gods who want to take me to fly.In a good mood, I have to write two articles to save my personal qualities for myself,Hope there will be good news tomorrow!

Well, you must be optimistic about yourself. If you don't want to do anything, how can others pull it? You are just a pupil who cannot help you! (Nima, saying that more and more primary school students have recently started, and I am about to quit the game...)


========================================================== ============


Without knowing it, it's useless to pull so much. Haha (you still need to pull it when it's useless ?)


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


1. When iconv is used, the engine also provides this library, but it is only a win32 platform, and you have to download the iconv library compilation after porting it to android.


2. Write the string to the xml file and parse the xml file. The format follows strings. xml in android. This is a better method,Especially when international support is required.


Anyway, I like the second one. Why? Because the first method has never been used ~~

Okay, because I have learned to parse xml in the android app before, And I am familiar with this, so I will introduce this.


To put it simply:


SAX is a faster and more effective method. It scans documents one by one and parses them one by one.


The cocos engine provides the SAXParser to parse xml. 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); // setDelegator void setDelegator (SAXDelegator * delegator) needs to be set; // resolution method, you need to override the following three methods // start a node static void startElement (void * ctx, const CC_XML_CHAR * name, const CC_XML_CHAR ** atts ); // end a node static void endElement (void * ctx, const CC_XML_CHAR * name); // The static void textHandler (void * ctx, const CC_XML_CHAR * name, int len );};


Well, we need Set the Delegator,The Delegator class is as follows, Three methods need to be rewritten.


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, encapsulate an XMLParser class according to the xml format. For example, the strings. xml


 
     
  
   
Xiaohuang battle
      
  
   
Reminder
      
  
   
Are you sure you want to quit?
      
  
   
OK
      
  
   
Return
  
 


Then implement an XMLParser class by yourself


# Pragma once # include
 
  
# Include "cocos2d. h "class XMLParser: public cocos2d: Ref, public cocos2d: SAXDelegator {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 the character, it can be used to read the xml data bool initWithString (const char * content) in the network; // start with the corresponding xml tag, for example:
  
   
Virtual void startElement (void * ctx, const char * name, const char ** atts); // end the corresponding xml tag, for example:
  Virtual void endElement (void * ctx, const char * name); // corresponds to the xml tag 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 ;};
 

Specific implementation:


# Include "XMLParser. h "using namespace std; using namespace cocos2d; // character ascii code // SPACE const static int SPACE = 32; // line feed const static int NEXTLINE = 10; // tab horizontal TAB const static int tab = 9; XMLParser * XMLParser: parseWithFile (const char * xmlFileName) {XMLParser * pXMLParser = new XMLParser (); if (pXMLParser-> initWithFile (xmlFileName) {pXMLParser-> autorelser (); return pXMLParser;} CC_SAFE_DELETE (pXMLParser); return NULL;} bool XMLParser :: initWithFile (const char * xmlFileName) {m_pDictionary = new CCDictionary (); SAXParser _ parser; _ parser. setDelegator (this); // get the full file path string fullPath = FileUtils: getInstance ()-> fullPathForFilename (xmlFileName); CCLog ("xml parser full path: % s ", fullPath. c_str (); return _ parser. parse (fullPath);} XMLParser * XMLParser: parseWithString (const char * content) {XMLParser * pXMLParser = new XMLParser (); if (pXMLParser-> initWithString (content )) {pXMLParser-> autorelease (); return pXMLParser;} transform (pXMLParser); return NULL;} bool XMLParser: initWithString (const char * content) {m_pDictionary = new CCDictionary (); SAXParser _ parse; _ parse. setDelegator (this); return _ parse. parse (content, strlen (content);} // start a node // for Example
 
  
Xiaohuang battle
 // The name is: string // atts [0]. The property: name // atts [1] is the value: app_name // 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 * atts Key = * 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 ); // whether all are 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 );}

Then it is easy to use.


XMLParser *pXmlParser = XMLParser::parseWithFile("strings.xml");String *pTitle = pXmlParser->getString("exit_dialog_title");



Well, that's it. Good night!

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.