From: http://www.jb51.cc/article/p-twvxsamc-dk.html
原文地址:http://blog.csdn.net/comeontom/article/details/7933692】
这些天被cocos2dx如何读取xml困惑着,现在总结总结,如有错误,欢迎指正!
先新建一个cocos2dx的工程
HelloWorldScene.cpp中的代码如下:
- #include"HelloWorldScene.h" @H_403_50@
- @H_403_50@
- usingnamespacecocos2d;@H_403_50@
- @H_403_50@
- CCScene*HelloWorld::scene()@H_403_50@
- {@H_403_50@
- CCScene*scene=NULL;@H_403_50@
- do@H_403_50@
- {@H_403_50@
- //'scene'isanautoreleaSEObject @H_403_50@
- scene=CCScene::create();@H_403_50@
- CC_BREAK_IF(!scene);@H_403_50@
- @H_403_50@
- //'layer'isanautoreleaSEObject @H_403_50@
- HelloWorld*layer=HelloWorld::create();@H_403_50@
- CC_BREAK_IF(!layer);@H_403_50@
- @H_403_50@
- //addlayerasachildtoscene @H_403_50@
- scene->addChild(layer);@H_403_50@
- }while(0);@H_403_50@
- @H_403_50@
- //returnthescene @H_403_50@
- returnscene;@H_403_50@
- }@H_403_50@
- @H_403_50@
- //on"init"youneedtoinitializeyourinstance @H_403_50@
- boolHelloWorld::init()@H_403_50@
- {@H_403_50@
- boolbRet=false;@H_403_50@
- do@H_403_50@
- {@H_403_50@
- ////////////////////////////////////////////////////////////////////////// @H_403_50@
- //superinitfirst @H_403_50@
- ////////////////////////////////////////////////////////////////////////// @H_403_50@
- @H_403_50@
- CC_BREAK_IF(!CCLayer::init());@H_403_50@
- @H_403_50@
- ////////////////////////////////////////////////////////////////////////// @H_403_50@
- //addyourcodesbelow... @H_403_50@
- ////////////////////////////////////////////////////////////////////////// @H_403_50@
- @H_403_50@
- //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram. @H_403_50@
- @H_403_50@
- //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject. @H_403_50@
- CCMenuItemImage*pCloseItem=CCMenuItemImage::create(@H_403_50@
- "CloseNormal.png",@H_403_50@
- "CloseSelected.png",@H_403_50@
- this,@H_403_50@
- menu_selector(HelloWorld::menuCloseCallback));@H_403_50@
- CC_BREAK_IF(!pCloseItem);@H_403_50@
- @H_403_50@
- //Placethemenuitembottom-rightconner. @H_403_50@
- pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));@H_403_50@
- @H_403_50@
- //Createamenuwiththe"close"menuitem,it'sanautoreleaSEObject. @H_403_50@
- CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);@H_403_50@
- pMenu->setPosition(CCPointZero);@H_403_50@
- CC_BREAK_IF(!pMenu);@H_403_50@
- @H_403_50@
- //AddthemenutoHelloWorldlayerasachildlayer. @H_403_50@
- this->addChild(pMenu,1);@H_403_50@
- @H_403_50@
- //2.Addalabelshows"HelloWorld". @H_403_50@
- @H_403_50@
- //Createalabelandinitializewithstring"HelloWorld". @H_403_50@
- @H_403_50@
- //最外面的CCDictionary @H_403_50@
- CCDictionary*pDict=CCDictionary::createWithContentsOfFile("strings.xml");@H_403_50@
- @H_403_50@
- //第二层CCDictionary @H_403_50@
- CCDictionary*pDict_2=newCCDictionary();@H_403_50@
- pDict_2->retain();@H_403_50@
- pDict_2=(CCDictionary*)pDict->objectForKey("special");@H_403_50@
- /*@H_403_50@
- 如果在同一个key下面存在<string>hhhhh</string>@H_403_50@
- <string>comeontom</string>@H_403_50@
- <true></true>@H_403_50@
- 这些关键词,则输出最后一个@H_403_50@
- */@H_403_50@
- CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString());@H_403_50@
- @H_403_50@
- /*第三层下面说的是Array中包含string@H_403_50@
- <key>special_2</key>@H_403_50@
- <array>@H_403_50@
- <string>comeontom1</string>@H_403_50@
- <string>comeontom2</string>@H_403_50@
- <string>comeontom3</string>@H_403_50@
- </array>@H_403_50@
- */@H_403_50@
- CCArray*pArray2=newCCArray();@H_403_50@
- pArray2->retain();@H_403_50@
- pArray2=(CCArray*)pDict_2->objectForKey("special_2");@H_403_50@
- for(inti=0;i<pArray2->count();i++)@H_403_50@
- {@H_403_50@
- CCLOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString());@H_403_50@
- }@H_403_50@
- @H_403_50@
- /*第三层下面说的是Array中包含string@H_403_50@
- <key>special_3</key>@H_403_50@
- <array>@H_403_50@
- <integer>45.0</integer>@H_403_50@
- <integer>3</integer>@H_403_50@
- <integer>43.444</integer>@H_403_50@
- </array>@H_403_50@
- */@H_403_50@
- CCArray*pArray3=newCCArray();@H_403_50@
- pArray3->retain();@H_403_50@
- pArray3=(CCArray*)pDict_2->objectForKey("special_3");@H_403_50@
- for(inti=0;i<pArray3->count();i++)@H_403_50@
- {@H_403_50@
- CCLOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());@H_403_50@
- }@H_403_50@
- /*第三层@H_403_50@
- <key>special_4</key>@H_403_50@
- <real>多媒体</real>@H_403_50@
- */@H_403_50@
- CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());@H_403_50@
- /*第三层@H_403_50@
- <key>special_5</key>@H_403_50@
- <false></false>@H_403_50@
- */@H_403_50@
- CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_5"))->getCString());@H_403_50@
- @H_403_50@
- @H_403_50@
- @H_403_50@
- CCLOG("strings.xmlCounts:%d",pDict->count());@H_403_50@
- CCLOG("pDict:%s",pDict);@H_403_50@
- CCArray*pArray=newCCArray();@H_403_50@
- pArray=pDict->allKeys();//把所有的键值付给pArray @H_403_50@
- for(inti=0;i<pArray->count();i++)@H_403_50@
- {@H_403_50@
- CCLOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());@H_403_50@
- }@H_403_50@
- @H_403_50@
- @H_403_50@
- CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),"Arial",24);@H_403_50@
- /*CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),"Arial",24);*/@H_403_50@
- @H_403_50@
- //CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld",24); @H_403_50@
- CC_BREAK_IF(!pLabel);@H_403_50@
- @H_403_50@
- //Getwindowsizeandplacethelabelupper. @H_403_50@
- CCSizesize=CCDirector::sharedDirector()->getWinSize();@H_403_50@
- pLabel->setPosition(ccp(size.width/2,size.height-50));@H_403_50@
- @H_403_50@
- //AddthelabeltoHelloWorldlayerasachildlayer. @H_403_50@
- this->addChild(pLabel,1);@H_403_50@
- @H_403_50@
- //3.Addaddasplashscreen,showthecocos2dsplashimage. @H_403_50@
- CCSprite*pSprite=CCSprite::create("HelloWorld.png");@H_403_50@
- CC_BREAK_IF(!pSprite);@H_403_50@
- @H_403_50@
- //Placethespriteonthecenterofthescreen @H_403_50@
- pSprite->setPosition(ccp(size.width/2,size.height/2));@H_403_50@
- @H_403_50@
- //AddthespritetoHelloWorldlayerasachildlayer. @H_403_50@
- this->addChild(pSprite,0);@H_403_50@
- @H_403_50@
- bRet=true;@H_403_50@
- }while(0);@H_403_50@
- @H_403_50@
- returnbRet;@H_403_50@
- }@H_403_50@
- @H_403_50@
- voidHelloWorld::menuCloseCallback(CCObject*pSender)@H_403_50@
- {@H_403_50@
- //"close"menuitemclicked @H_403_50@
- CCDirector::sharedDirector()->end();@H_403_50@
- }@H_403_50@
#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 instance bool 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代码如下:
- <?xmlversion="1.0"encoding="UTF-8"?>@H_403_50@
- <!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">@H_403_50@
- <plistversion="1.0">@H_403_50@
- <dict>@H_403_50@
- <key>special</key>@H_403_50@
- <string>@H_403_50@
- <dict>@H_403_50@
- <key>special_1</key>@H_403_50@
- <string>hhhhh</string>@H_403_50@
- <string>comeontom</string>@H_403_50@
- <true></true>@H_403_50@
- <key>special_2</key>@H_403_50@
- <array>@H_403_50@
- <string>comeontom1</string>@H_403_50@
- <string>comeontom2</string>@H_403_50@
- <string>comeontom3</string>@H_403_50@
- </array>@H_403_50@
- <key>special_3</key>@H_403_50@
- <array>@H_403_50@
- <integer>45.0</integer>@H_403_50@
- <integer>3</integer>@H_403_50@
- <integer>43.444</integer>@H_403_50@
- </array>@H_403_50@
- <key>special_4</key>@H_403_50@
- <real>多媒体</real>@H_403_50@
- <key>special_5</key>@H_403_50@
- <false></false>@H_403_50@
- </dict>@H_403_50@
- </string>@H_403_50@
- <key>chinese1</key>@H_403_50@
- <string>美好的一天</string>@H_403_50@
- <key>japanese</key>@H_403_50@
- <string>良い一日を</string>@H_403_50@
- <key>spanish</key>@H_403_50@
- <string>Buendía</string>@H_403_50@
- </dict>@H_403_50@
- </plist>@H_403_50@
<?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)