CCDictionary 解析xml总结

先新建一个cocos2dx的工程

HelloWorldScene.cpp中的代码如下:

  1. #include "HelloWorldScene.h"  
  2.   
  3. using namespace cocos2d;  
  4.   
  5. CCScene* HelloWorld::scene()  
  6. {  
  7.     CCScene * scene = NULL;  
  8.     do   
  9.     {  
  10.         // 'scene' is an autorelease object  
  11.         scene = CCScene::create();  
  12.         CC_BREAK_IF(! scene);  
  13.   
  14.         // 'layer' is an autorelease object  
  15.         HelloWorld *layer = HelloWorld::create();  
  16.         CC_BREAK_IF(! layer);  
  17.   
  18.         // add layer as a child to scene  
  19.         scene->addChild(layer);  
  20.     } while (0);  
  21.   
  22.     // return the scene  
  23.     return scene;  
  24. }  
  25.   
  26. // on "init" you need to initialize your instance  
  27. bool HelloWorld::init()  
  28. {  
  29.     bool bRet = false;  
  30.     do   
  31.     {  
  32.         //  
  33.         // super init first  
  34.         //  
  35.   
  36.         CC_BREAK_IF(! CCLayer::init());  
  37.   
  38.         //  
  39.         // add your codes below...  
  40.         //  
  41.   
  42.         // 1. Add a menu item with "X" image, which is clicked to quit the program.  
  43.   
  44.         // Create a "close" menu item with close icon, it's an auto release object.  
  45.         CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  46.             "CloseNormal.png",  
  47.             "CloseSelected.png",  
  48.             this,  
  49.             menu_selector(HelloWorld::menuCloseCallback));  
  50.         CC_BREAK_IF(! pCloseItem);  
  51.   
  52.         // Place the menu item bottom-right conner.  
  53.         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));  
  54.   
  55.         // Create a menu with the "close" menu item, it's an auto release object.  
  56.         CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  57.         pMenu->setPosition(CCPointZero);  
  58.         CC_BREAK_IF(! pMenu);  
  59.   
  60.         // Add the menu to HelloWorld layer as a child layer.  
  61.         this->addChild(pMenu, 1);  
  62.   
  63.         // 2. Add a label shows "Hello World".  
  64.   
  65.         // Create a label and initialize with string "Hello World".  
  66.   
  67.         //最外面的 CCDictionary  
  68.         CCDictionary* pDict = CCDictionary::createWithContentsOfFile("strings.xml");  
  69.   
  70.         //第二层 CCDictionary  
  71.         CCDictionary* pDict_2 = new CCDictionary();  
  72.         pDict_2->retain();  
  73.         pDict_2 = (CCDictionary*)pDict->objectForKey("special");  
  74.         /* 
  75.         如果在同一个key下面存在<string>hhhhh</string> 
  76.                                <string>comeontom</string> 
  77.                                <true></true> 
  78.         这些关键词,则输出最后一个 
  79.         */  
  80.         CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString());  
  81.   
  82.         /*第三层  下面说的是Array中包含string 
  83.         <key>special_2</key> 
  84.         <array> 
  85.             <string>comeontom1</string> 
  86.             <string>comeontom2</string> 
  87.             <string>comeontom3</string> 
  88.         </array> 
  89.         */  
  90.         CCArray* pArray2 = new CCArray();  
  91.         pArray2->retain();  
  92.         pArray2 = (CCArray*)pDict_2->objectForKey("special_2");  
  93.         for (int i = 0;i < pArray2->count();i++)  
  94.         {  
  95.             CCLOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString());  
  96.         }  
  97.   
  98.         /*第三层  下面说的是Array中包含string  
  99.         <key>special_3</key> 
  100.         <array> 
  101.             <integer>45.0</integer> 
  102.             <integer>3</integer> 
  103.             <integer>43.444</integer> 
  104.         </array> 
  105.         */  
  106.         CCArray* pArray3 = new CCArray();  
  107.         pArray3->retain();  
  108.         pArray3 = (CCArray*)pDict_2->objectForKey("special_3");  
  109.         for (int i = 0;i < pArray3->count();i++)  
  110.         {  
  111.             CCLOG("pArray3%i:%s",i+1,((CCString*)pArray3->objectAtIndex(i))->getCString());  
  112.         }  
  113.         /*第三层 
  114.         <key>special_4</key> 
  115.         <real>多媒体</real> 
  116.         */  
  117.         CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());  
  118.         /*第三层 
  119.         <key>special_5</key> 
  120.         <false></false> 
  121.         */  
  122.         CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_5"))->getCString());  
  123.   
  124.   
  125.   
  126.         CCLOG("strings.xmlCounts:%d",pDict->count());  
  127.         CCLOG("pDict:%s",pDict);  
  128.         CCArray* pArray = new CCArray();  
  129.         pArray = pDict->allKeys();//把所有的键值付给pArray  
  130.         for (int i = 0;i < pArray->count();i++)  
  131.         {  
  132.             CCLOG("pArray%i:%s",i+1,((CCString*)pArray->objectAtIndex(i))->getCString());  
  133.         }  
  134.               
  135.       
  136.         CCLabelTTF* pLabel = CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(), "Arial", 24);  
  137.         /*CCLabelTTF* pLabel = CCLabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(), "Arial", 24);*/  
  138.   
  139.        // CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);  
  140.         CC_BREAK_IF(! pLabel);  
  141.   
  142.         // Get window size and place the label upper.   
  143.         CCSize size = CCDirector::sharedDirector()->getWinSize();  
  144.         pLabel->setPosition(ccp(size.width / 2, size.height - 50));  
  145.   
  146.         // Add the label to HelloWorld layer as a child layer.  
  147.         this->addChild(pLabel, 1);  
  148.   
  149.         // 3. Add add a splash screen, show the cocos2d splash image.  
  150.         CCSprite* pSprite = CCSprite::create("HelloWorld.png");  
  151.         CC_BREAK_IF(! pSprite);  
  152.   
  153.         // Place the sprite on the center of the screen  
  154.         pSprite->setPosition(ccp(size.width/2, size.height/2));  
  155.   
  156.         // Add the sprite to HelloWorld layer as a child layer.  
  157.         this->addChild(pSprite, 0);  
  158.           
  159.         bRet = true;  
  160.     } while (0);  
  161.   
  162.     return bRet;  
  163. }  
  164.   
  165. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  166. {  
  167.     // "close" menu item clicked  
  168.     CCDirector::sharedDirector()->end();  
  169. }  


strings.xml代码如下:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
  3. <plist version="1.0">  
  4. <dict>  
  5.   <key>special</key>  
  6.   <string>  
  7.     <dict>  
  8.         <key>special_1</key>  
  9.         <string>hhhhh</string>  
  10.         <string>comeontom</string>  
  11.         <true></true>  
  12.         <key>special_2</key>  
  13.         <array>  
  14.             <string>comeontom1</string>  
  15.             <string>comeontom2</string>  
  16.             <string>comeontom3</string>  
  17.         </array>  
  18.         <key>special_3</key>  
  19.         <array>  
  20.             <integer>45.0</integer>  
  21.             <integer>3</integer>  
  22.             <integer>43.444</integer>  
  23.         </array>  
  24.         <key>special_4</key>  
  25.         <real>多媒体</real>  
  26.         <key>special_5</key>  
  27.         <false></false>  
  28.     </dict>  
  29.   </string>  
  30.     <key>chinese1</key>  
  31.     <string>美好的一天</string>  
  32.     <key>japanese</key>  
  33.     <string>良い一日を</string>  
  34.     <key>spanish</key>  
  35.     <string>Buen día</string>  
  36. </dict>  
  37. </plist>  


最后来个总结

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值