cocos2dx读取xml详细解析

cocos2dx读取xml详细解析,第1张

概述From: http://www.voidcn.com/article/p-twvxsamc-dk.html 原文地址:http://blog.csdn.net/comeontom/article/details/7933692】 这些天被cocos2dx如何读取xml困惑着,现在总结总结,如有错误,欢迎指正! 先新建一个cocos2dx的工程 HelloWorldScene.cpp中的代码如下:

From: http://www.jb51.cc/article/p-twvxsamc-dk.html

@H_419_3@原文地址:http://blog.csdn.net/comeontom/article/details/7933692

@H_419_3@这些天被cocos2dx如何读取xml困惑着,现在总结总结,如有错误,欢迎指正!

@H_419_3@先新建一个cocos2dx的工程

@H_419_3@HelloWorldScene.cpp中的代码如下:

@H_419_3@

[cpp] view plain copy print ? #include"HelloWorldScene.h" usingnamespacecocos2d; CCScene*HelloWorld::scene() { CCScene*scene=NulL; do { //'scene'isanautoreleaSEObject scene=CCScene::create(); CC_BREAK_IF(!scene); //'layer'isanautoreleaSEObject HelloWorld*layer=HelloWorld::create(); CC_BREAK_IF(!layer); //addlayerasachildtoscene scene->addChild(layer); }while(0); //returnthescene returnscene; } //on"init"youneedtoinitializeyourinstance boolHelloWorld::init() { boolbRet=false; do { ////////////////////////////////////////////////////////////////////////// //superinitfirst ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(!cclayer::init()); ////////////////////////////////////////////////////////////////////////// //addyourcodesbelow... ////////////////////////////////////////////////////////////////////////// //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram. //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject. CcmenuItemImage*pCloseItem=CcmenuItemImage::create( "Closenormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(!pCloseItem); //Placethemenuitembottom-rightconner. pCloseItem->setposition(ccp(CCDirector::sharedDirector()->getWinSize().wIDth-20,20)); //Createamenuwiththe"close"menuitem,it'sanautoreleaSEObject. Ccmenu*pMenu=Ccmenu::create(pCloseItem,NulL); pMenu->setposition(CCPointZero); CC_BREAK_IF(!pMenu); //AddthemenutoHelloWorldlayerasachildlayer. this->addChild(pMenu,1); //2.Addalabelshows"HelloWorld". //Createalabelandinitializewithstring"HelloWorld". //最外面的CCDictionary CCDictionary*pDict=CCDictionary::createWithContentsOffile("strings.xml"); //第二层CCDictionary CCDictionary*pDict_2=newCCDictionary(); pDict_2->retain(); pDict_2=(CCDictionary*)pDict->objectForKey("special"); /* 如果在同一个key下面存在<string>hhhhh</string> <string>comeontom</string> <true></true> 这些关键词,则输出最后一个 */ cclOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString()); /*第三层下面说的是Array中包含string <key>special_2</key> <array> <string>comeontom1</string> <string>comeontom2</string> <string>comeontom3</string> </array> */ CCArray*pArray2=newCCArray(); pArray2->retain(); pArray2=(CCArray*)pDict_2->objectForKey("special_2"); for(inti=0;i<pArray2->count();i++) { cclOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString()); } /*第三层下面说的是Array中包含string <key>special_3</key> <array> <integer>45.0</integer> <integer>3</integer> <integer>43.444</integer> </array> */ CCArray*pArray3=newCCArray(); pArray3->retain(); pArray3=(CCArray*)pDict_2->objectForKey("special_3"); for(inti=0;i<pArray3->count();i++) { cclOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString()); } /*第三层 <key>special_4</key> <real>多媒体</real> */ cclOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString()); /*第三层 <key>special_5</key> <false></false> */ cclOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_5"))->getCString()); cclOG("strings.xmlCounts:%d",pDict->count()); cclOG("pDict:%s",pDict); CCArray*pArray=newCCArray(); pArray=pDict->allKeys();//把所有的键值付给pArray for(inti=0;i<pArray->count();i++) { cclOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString()); } cclabelTTF*pLabel=cclabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),"Arial",24); /*cclabelTTF*pLabel=cclabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),"Arial",24);*/ //cclabelTTF*pLabel=cclabelTTF::create("HelloWorld",24); CC_BREAK_IF(!pLabel); //Getwindowsizeandplacethelabelupper. CCSizesize=CCDirector::sharedDirector()->getWinSize(); pLabel->setposition(ccp(size.wIDth/2,size.height-50)); //AddthelabeltoHelloWorldlayerasachildlayer. this->addChild(pLabel,1); //3.Addaddasplashscreen,showthecocos2dsplashimage. CCSprite*pSprite=CCSprite::create("HelloWorld.png"); CC_BREAK_IF(!pSprite); //Placethespriteonthecenterofthescreen pSprite->setposition(ccp(size.wIDth/2,size.height/2)); //AddthespritetoHelloWorldlayerasachildlayer. this->addChild(pSprite,0); bRet=true; }while(0); returnbRet; } voIDHelloWorld::menuCloseCallback(CCObject*pSender) { //"close"menuitemclicked CCDirector::sharedDirector()->end(); }
#include "HelloWorldScene.h"using namespace cocos2d;CCScene* HelloWorld::scene(){    CCScene * scene = NulL;    do     {        // 'scene' is an autorelease object        scene = CCScene::create();        CC_BREAK_IF(! scene);        // 'layer' is an autorelease object        HelloWorld *layer = HelloWorld::create();        CC_BREAK_IF(! layer);        // add layer as a child to scene        scene->addChild(layer);    } while (0);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    bool bRet = false;    do     {        //////////////////////////////////////////////////////////////////////////        // super init first        //////////////////////////////////////////////////////////////////////////        CC_BREAK_IF(! cclayer::init());        //////////////////////////////////////////////////////////////////////////        // add your codes below...        //////////////////////////////////////////////////////////////////////////        // 1. Add a menu item with "X" image,which is clicked to quit the program.        // Create a "close" menu item with close icon,it's an auto release object.        CcmenuItemImage *pCloseItem = CcmenuItemImage::create(            "Closenormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));        CC_BREAK_IF(! pCloseItem);        // Place the menu item bottom-right conner.        pCloseItem->setposition(ccp(CCDirector::sharedDirector()->getWinSize().wIDth - 20,20));        // Create a menu with the "close" menu item,it's an auto release object.        Ccmenu* pMenu = Ccmenu::create(pCloseItem,NulL);        pMenu->setposition(CCPointZero);        CC_BREAK_IF(! pMenu);        // Add the menu to HelloWorld layer as a child layer.        this->addChild(pMenu,1);        // 2. Add a label shows "Hello World".        // Create a label and initialize with string "Hello World".		//最外面的 CCDictionary		CCDictionary* pDict = CCDictionary::createWithContentsOffile("strings.xml");		//第二层 CCDictionary		CCDictionary* pDict_2 = new CCDictionary();		pDict_2->retain();		pDict_2 = (CCDictionary*)pDict->objectForKey("special");		/*		如果在同一个key下面存在<string>hhhhh</string>							   <string>comeontom</string>							   <true></true>		这些关键词,则输出最后一个		*/		cclOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString());		/*第三层  下面说的是Array中包含string		<key>special_2</key>		<array>			<string>comeontom1</string>			<string>comeontom2</string>			<string>comeontom3</string>		</array>		*/		CCArray* pArray2 = new CCArray();		pArray2->retain();		pArray2 = (CCArray*)pDict_2->objectForKey("special_2");		for (int i = 0;i < pArray2->count();i++)		{			cclOG("pArray2%i:%s",((CCString*)pArray2->objectAtIndex(i))->getCString());		}		/*第三层  下面说的是Array中包含string 		<key>special_3</key>		<array>			<integer>45.0</integer>			<integer>3</integer>			<integer>43.444</integer>		</array>		*/		CCArray* pArray3 = new CCArray();		pArray3->retain();		pArray3 = (CCArray*)pDict_2->objectForKey("special_3");		for (int i = 0;i < pArray3->count();i++)		{			cclOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());		}		/*第三层		<key>special_4</key>		<real>多媒体</real>		*/		cclOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());		/*第三层		<key>special_5</key>		<false></false>		*/		cclOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_5"))->getCString());		cclOG("strings.xmlCounts:%d",pDict->count());		cclOG("pDict:%s",pDict);		CCArray* pArray = new CCArray();		pArray = pDict->allKeys();//把所有的键值付给pArray		for (int i = 0;i < pArray->count();i++)		{			cclOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());		}						cclabelTTF* pLabel = cclabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),24);		/*cclabelTTF* pLabel = cclabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),24);*/       // cclabelTTF* pLabel = cclabelTTF::create("Hello World",24);        CC_BREAK_IF(! pLabel);        // Get window size and place the label upper.         CCSize size = CCDirector::sharedDirector()->getWinSize();        pLabel->setposition(ccp(size.wIDth / 2,size.height - 50));        // Add the label to HelloWorld layer as a child layer.        this->addChild(pLabel,1);        // 3. Add add a splash screen,show the cocos2d splash image.        CCSprite* pSprite = CCSprite::create("HelloWorld.png");        CC_BREAK_IF(! pSprite);        // Place the sprite on the center of the screen        pSprite->setposition(ccp(size.wIDth/2,size.height/2));        // Add the sprite to HelloWorld layer as a child layer.        this->addChild(pSprite,0);		        bRet = true;    } while (0);    return bRet;}voID HelloWorld::menuCloseCallback(CCObject* pSender){    // "close" menu item clicked    CCDirector::sharedDirector()->end();}


strings.xml代码如下:

[HTML] view plain copy print ? <?xmlversion="1.0"enCoding="UTF-8"?> <!DOCTYPEpListPUBliC"-//Apple//DTDPList1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <pListversion="1.0"> <dict> <key>special</key> <string> <dict> <key>special_1</key> <string>hhhhh</string> <string>comeontom</string> <true></true> <key>special_2</key> <array> <string>comeontom1</string> <string>comeontom2</string> <string>comeontom3</string> </array> <key>special_3</key> <array> <integer>45.0</integer> <integer>3</integer> <integer>43.444</integer> </array> <key>special_4</key> <real>多媒体</real> <key>special_5</key> <false></false> </dict> </string> <key>chinese1</key> <string>美好的一天</string> <key>japanese</key> <string>良い一日を</string> <key>spanish</key> <string>Buendía</string> </dict> </pList>
<?xml version="1.0" enCoding="UTF-8"?><!DOCTYPE pList PUBliC "-//Apple//DTD PList 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><pList version="1.0"><dict>  <key>special</key>  <string>  	<dict>		<key>special_1</key>		<string>hhhhh</string>		<string>comeontom</string>		<true></true>		<key>special_2</key>		<array>			<string>comeontom1</string>			<string>comeontom2</string>			<string>comeontom3</string>		</array>		<key>special_3</key>		<array>			<integer>45.0</integer>			<integer>3</integer>			<integer>43.444</integer>		</array>		<key>special_4</key>		<real>多媒体</real>		<key>special_5</key>		<false></false>	</dict>  </string>	<key>chinese1</key>	<string>美好的一天</string>	<key>japanese</key>	<string>良い一日を</string>	<key>spanish</key>	<string>Buen día</string></dict></pList>


最后来个总结:

dict:建立字典
key:字典里面的关键词
integer:整形,其实和string、real的功能差不多,起配对作用
real:和integer、string的功能差不多,起配对作用
true:<true></true>如果这样写,输出的是1
false:<false></false> 输出的是0
string:和integer、real的功能差不多,起配对作用
array:建立数组


当然了,有兴趣看源码的同学,可以看这个文件:CCfileUtilsCommon_cpp.h(位置:cocos2dx\platform)

总结

以上是内存溢出为你收集整理的cocos2dx读取xml详细解析全部内容,希望文章能够帮你解决cocos2dx读取xml详细解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1010764.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存