When we get the network data, we basically use XML and JSON, so learning to parse these two kinds of data is very helpful to us.
Here's the code:
HelloParse.h:
<span style= "FONT-SIZE:18PX;" > #ifndef __helloparse_h__#define __helloparse_h__#include "cocos2d.h" using namespace Cocos2d;class helloparse: Public cocos2d::layer{public:static cocos2d::scene* Createparsescene (); virtual bool init (); void Makejson (); void Parsejson (); void Makexml (const char *filename); void Parsexml (const char *filename); Create_func (helloparse);}; #endif//!__hellomap_h__</span>
HelloParse.cpp:
#include "HelloParse.h" #include "json/document.h" #include "json/writer.h" #include "json/stringbuffer.h" #include " Json/rapidjson.h "#include" tinyxml2/tinyxml2.h "using namespace tinyxml2;using namespace Rapidjson; USING_NS_CC; scene* helloparse::createparsescene () {Auto Mapscene = scene::create (); Auto Maplayer = Helloparse::create (); mapScene- >addchild (Maplayer); return mapscene;} BOOL Helloparse::init () {if (! Layer::init ()) {return false;} Makejson ();p Arsejson (), Makexml ("Lake.xml");p arsexml ("Lake.xml"); return true;} void Helloparse::makejson () {/* generates json*///to obtain a utf-8 encoded file object Rapidjson::D ocument document;//set an empty object document. SetObject ();//Get Allocator Rapidjson::D ocument::allocatortype& allocator = Document. Getallocator ();//define array and Object Rapidjson::value array (rapidjson::karraytype); Rapidjson::value object (Rapidjson:: Kobjecttype);//Add data member object. AddMember ("int", 1, allocator), object. AddMember ("Double", 1.0, allocator), object. AddMember ("bool", true, allocator), object. AddMember ("Hello", "Hello", allocator);//Will beforeThe added data is put into an array of arrays. Pushback (object, allocator);//Add member document to file object. AddMember ("JSON", "JSON string", allocator);d ocument. AddMember ("array", array, allocator);//allocate buffer, write file contents to buffer StringBuffer buffer;rapidjson::writer<stringbuffer> Writer (buffer);d ocument. Accept (writer); Cclog ("%s", buffer. GetString ());} void Helloparse::p Arsejson () {/* parse json*/std::string str = "{\" hello\ ": \" word\ "}"; Cclog ("%s\n", Str.c_str ()); Rapidjson::D ocument d;//The data is stored in JSON format to DD. Parse<0> (Str.c_str ()); if (D.hasparseerror ())//Print Parse error {cclog ("Getparseerror%s\n", D.getparseerror ());} Gets the value of the key for Hello if (d.isobject () && d.hasmember ("Hello")) {Cclog ("%s\n", d["Hello"]. GetString ());//print Gets the value of Hello}}void helloparse::makexml (const char *filename) {//Get writable path plus filename std::string filePath = Fileutils::getinstance ()->getwritablepath () + filename;//Create an XML file tinyxml2::xmldocument* PDoc = new TINYXML2:: XMLDocument ();//xml declaration (optional parameter) XmlDeclaration *pdel = pdoc->newdeclaration ("xml version=\" 1.0\ "encoding=\" utf-8\ "");//Add XML key-value pair node end pdoc->linkendchild (Pdel);//Add plist node XmlElement *plistelement = Pdoc->newelement ("plist");// Set Property Plistelement->setattribute ("Version", "1.0");pD Oc->linkendchild (plistelement); Xmlcomment *commentelement = Pdoc->newcomment ("This is XML comment");p Listelement->linkendchild (commentelement );//Add dic node XmlElement *dicelement = Pdoc->newelement ("dic");p Listelement->linkendchild (dicelement);// Add the key node XmlElement *keyelement = pdoc->newelement ("key"); Keyelement->linkendchild ("Text"); Dicelement->linkendchild (keyelement); XMLElement *arrayelement = pdoc->newelement ("array");d Icelement->linkendchild (arrayelement); for (int i = 0; i <3; i++) {XMLElement *elm = pdoc->newelement ("name"); Elm->linkendchild (Pdoc->newtext ("cocos2d-x")); Arrayelement->linkendchild (elm);} Writes the specified file Pdoc->savefile (filepath.c_str ());//Prints it on the screen pdoc->print ();d elete PDoc;} void Helloparse::p arsexml (const char *filename) {std::string FilePath =Fileutils::getinstance ()->getwritablepath () + Filename;log ("%s", Filepath.c_str ()); Tinyxml2::xmldocument *pDoc = New Tinyxml2::xmldocument ();//load specified directory file, format correctly return 0XMLError ErrorID = Pdoc->loadfile (Filepath.c_str ()); if (ErrorID! = 0 {//xml format error return;} Get the root node, i.e. <?xml version= "1.0" encoding= "UTF-8"? >xmlelement *rootele = Pdoc->rootelement ();//Gets the first node property of the const XmlAttribute *attribute = Rootele->firstattribute ();//Print node property name and value log ("Attribute_name =%s,attribute_value =%s", Attribute->name (), Attribute->value ());//Gets the value XmlElement *dicele = rootele->firstchildelement ("dic") based on key; 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;}
Operation Result:
To create a JSON:
{"JSON": "JSON string", "array": [{"int": 1, "Double": 1.0, "bool": true, "Hello": "???"}]}
{"Hello": "word"}
Parsing JSON:
Word
Create xml:
<?xml version= "1.0" encoding= "UTF-8"?>
<plist version= "1.0" >
<!--this is XML comment-->
<dic>
<key>Text</key>
<array>
<name>Cocos2d-x</name>
<name>Cocos2d-x</name>
<name>Cocos2d-x</name>
</array>
</dic>
</plist>
Parsing xml:
C:/users/administrator/appdata/local/helloworld/lake.xml
Attribute_name = Version,attribute_value = 1.0
Keyele text= Text
Childele text= Cocos2d-x
Childele text= Cocos2d-x
Childele text= Cocos2d-x
Copyright NOTICE: Reprint please indicate the source.
Cocos2d-x Study Notes (13)--Data analysis (Json/xml)