CCDictionary 解析xml总结

前端之家收集整理的这篇文章主要介绍了CCDictionary 解析xml总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://blog.csdn.net/jalen_yuchi/article/details/8617348


先新建一个cocos2dx的工程

HelloWorldScene.cpp中的代码如下:

  1. @H_502_37@#include"HelloWorldScene.h"
  2. @H_502_37@
  3. @H_502_37@usingnamespacecocos2d;
  4. @H_502_37@CCScene*HelloWorld::scene()
  5. @H_502_37@{
  6. @H_502_37@CCScene*scene=NULL;
  7. @H_502_37@do
  8. @H_502_37@{
  9. @H_502_37@//'scene'isanautoreleaSEObject
  10. @H_502_37@scene=CCScene::create();
  11. @H_502_37@CC_BREAK_IF(!scene);
  12. @H_502_37@
  13. //'layer'isanautoreleaSEObject
  14. @H_502_37@HelloWorld*layer=HelloWorld::create();
  15. @H_502_37@CC_BREAK_IF(!layer);
  16. //addlayerasachildtoscene
  17. @H_502_37@scene->addChild(layer);
  18. @H_502_37@}while(0);
  19. //returnthescene
  20. @H_502_37@returnscene;
  21. @H_502_37@}
  22. //on"init"youneedtoinitializeyourinstance
  23. @H_502_37@boolHelloWorld::init()
  24. boolbRet=false;
  25. //////////////////////////////////////////////////////////////////////////
  26. @H_502_37@//superinitfirst
  27. @H_502_37@CC_BREAK_IF(!CCLayer::init());
  28. //addyourcodesbelow...
  29. //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram.
  30. //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject.
  31. @H_502_37@CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
  32. @H_502_37@"CloseNormal.png",
  33. @H_502_37@"CloseSelected.png",85)"> @H_502_37@this,153); list-style:decimal-leading-zero outside; color:inherit"> @H_502_37@menu_selector(HelloWorld::menuCloseCallback));
  34. @H_502_37@CC_BREAK_IF(!pCloseItem);
  35. //Placethemenuitembottom-rightconner.
  36. @H_502_37@pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
  37. //Createamenuwiththe"close"menuitem,85)"> @H_502_37@CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
  38. @H_502_37@pMenu->setPosition(CCPointZero);
  39. @H_502_37@CC_BREAK_IF(!pMenu);
  40. //AddthemenutoHelloWorldlayerasachildlayer.
  41. @H_502_37@this->addChild(pMenu,1);
  42. //2.Addalabelshows"HelloWorld".
  43. //Createalabelandinitializewithstring"HelloWorld".
  44. //最外面的CCDictionary
  45. @H_502_37@CCDictionary*pDict=CCDictionary::createWithContentsOfFile("strings.xml");
  46. //第二层CCDictionary
  47. @H_502_37@CCDictionary*pDict_2=newCCDictionary();
  48. @H_502_37@pDict_2->retain();
  49. @H_502_37@pDict_2=(CCDictionary*)pDict->objectForKey("special");
  50. /*
  51. 如果在同一个key下面存在<string>hhhhh</string>
  52. <string>comeontom</string>
  53. <true></true>
  54. 这些关键词,则输出最后一个
  55. */
  56. @H_502_37@CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString());
  57. /*第三层下面说的是Array中包含string
  58. <key>special_2</key>
  59. <array>
  60. <string>comeontom1</string>
  61. <string>comeontom2</string>
  62. <string>comeontom3</string>
  63. </array>
  64. @H_502_37@CCArray*pArray2=newCCArray();
  65. @H_502_37@pArray2->retain();
  66. @H_502_37@pArray2=(CCArray*)pDict_2->objectForKey("special_2");
  67. @H_502_37@for(inti=0;i<pArray2->count();i++)
  68. @H_502_37@CCLOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString());
  69. <key>special_3</key>
  70. <integer>45.0</integer>
  71. <integer>3</integer>
  72. <integer>43.444</integer>
  73. @H_502_37@CCArray*pArray3=newCCArray();
  74. @H_502_37@pArray3->retain();
  75. @H_502_37@pArray3=(CCArray*)pDict_2->objectForKey("special_3");
  76. inti=0;i<pArray3->count();i++)
  77. @H_502_37@CCLOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());
  78. /*第三层
  79. <key>special_4</key>
  80. <real>多媒体</real>
  81. @H_502_37@CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());
  82. <key>special_5</key>
  83. <false></false>
  84. "special_5"))->getCString());
  85. @H_502_37@CCLOG("strings.xmlCounts:%d",pDict->count());
  86. @H_502_37@CCLOG("pDict:%s",pDict);
  87. @H_502_37@CCArray*pArray=newCCArray();
  88. @H_502_37@pArray=pDict->allKeys();//把所有的键值付给pArray
  89. @H_502_37@for(inti=0;i<pArray->count();i++)
  90. @H_502_37@CCLOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());
  91. @H_502_37@}
  92. @H_502_37@CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),"Arial",24);
  93. /*CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),"Arial",24);*/
  94. //CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld",24);
  95. @H_502_37@CC_BREAK_IF(!pLabel);
  96. //Getwindowsizeandplacethelabelupper.
  97. @H_502_37@CCSizesize=CCDirector::sharedDirector()->getWinSize();
  98. @H_502_37@pLabel->setPosition(ccp(size.width/2,size.height-50));
  99. //AddthelabeltoHelloWorldlayerasachildlayer.
  100. @H_502_37@this->addChild(pLabel,0); background-color:inherit">//3.Addaddasplashscreen,showthecocos2dsplashimage.
  101. @H_502_37@CCSprite*pSprite=CCSprite::create("HelloWorld.png");
  102. @H_502_37@CC_BREAK_IF(!pSprite);
  103. //Placethespriteonthecenterofthescreen
  104. @H_502_37@pSprite->setPosition(ccp(size.width/2,size.height/2));
  105. //AddthespritetoHelloWorldlayerasachildlayer.
  106. @H_502_37@this->addChild(pSprite,0);
  107. @H_502_37@bRet=true;
  108. @H_502_37@returnbRet;
  109. @H_502_37@voidHelloWorld::menuCloseCallback(CCObject*pSender)
  110. //"close"menuitemclicked
  111. @H_502_37@CCDirector::sharedDirector()->end();
  112. @H_502_37@}


strings.xml代码如下:

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

最后来个总结

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

原文链接:https://www.f2er.com/xml/298990.html

猜你在找的XML相关文章