先要做准备工作

先下载DS_Dictionary.hDs_Dictionary.cpp,不多说提供下载地址

http://www.cocos2d-x.org/boards/6/topics/6125?r=13203#message-13203

文件基于pugixml下面是下载地址

http://pugixml.org/

 

下面提供test.plist作对照

 

   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?> 
  2. > 
  3. <plist version="1.0"> 
  4. <dict> 
  5.     <key>texturekey> 
  6.     <dict> 
  7.         <key>widthkey> 
  8.         <integer>512integer> 
  9.         <key>heightkey> 
  10.         <integer>1024integer> 
  11.     dict> 
  12.     <key>frameskey> 
  13.     <dict> 
  14.         <key>bg_0.pngkey> 
  15.         <dict> 
  16.             <key>xkey> 
  17.             <integer>1integer> 
  18.             <key>ykey> 
  19.             <integer>289integer> 
  20.             <key>widthkey> 
  21.             <integer>480integer> 
  22.             <key>heightkey> 
  23.             <integer>287integer> 
  24.             <key>offsetXkey> 
  25.             <real>0real> 
  26.             <key>offsetYkey> 
  27.             <real>1real> 
  28.             <key>originalWidthkey> 
  29.             <integer>480integer> 
  30.             <key>originalHeightkey> 
  31.             <integer>289integer> 
  32.         dict> 
  33.         <key>bg_2.pngkey> 
  34.         <dict> 
  35.             <key>xkey> 
  36.             <integer>1integer> 
  37.             <key>ykey> 
  38.             <integer>1integer> 
  39.             <key>widthkey> 
  40.             <integer>480integer> 
  41.             <key>heightkey> 
  42.             <integer>287integer> 
  43.             <key>offsetXkey> 
  44.             <real>0real> 
  45.             <key>offsetYkey> 
  46.             <real>0real> 
  47.             <key>originalWidthkey> 
  48.             <integer>480integer> 
  49.             <key>originalHeightkey> 
  50.             <integer>287integer> 
  51.         dict> 
  52.         <key>bg_1.pngkey> 
  53.         <dict> 
  54.             <key>xkey> 
  55.             <integer>1integer> 
  56.             <key>ykey> 
  57.             <integer>577integer> 
  58.             <key>widthkey> 
  59.             <integer>480integer> 
  60.             <key>heightkey> 
  61.             <integer>287integer> 
  62.             <key>offsetXkey> 
  63.             <real>0real> 
  64.             <key>offsetYkey> 
  65.             <real>0real> 
  66.             <key>originalWidthkey> 
  67.             <integer>480integer> 
  68.             <key>originalHeightkey> 
  69.             <integer>287integer> 
  70.         dict> 
  71.     dict> 
  72. dict> 
  73. plist> 

下面提供一个解析的函数

 

   
   
   
   
  1. void collision::plistParse() 
  2.     //Load rootDict from file, and then step into the metadata sub dict. 
  3.     DS_Dictionary rootDict; 
  4.     if(!rootDict.loadRootSubDictFromFile(bgObjectPlist)) 
  5.     {  
  6.             printf("no load\n");  
  7.     } 
  8.     if(!rootDict.stepIntoSubDictWithKey("texture")) 
  9.     {  
  10.            printf("No texture\n");  
  11.     } 
  12.     //Get an int value from the subdict 
  13.     int someInt = rootDict.getIntegerForKey("height"); 
  14.  
  15.     printf("height    %d\n",someInt); 
  16.  
  17.     someInt = rootDict.getIntegerForKey("width"); 
  18.  
  19.     printf("width    %d\n",someInt); 
  20.     //Step out of the sub dict and into another 
  21.     rootDict.stepOutOfSubDict(); 
  22.  
  23.     if(!rootDict.stepIntoSubDictWithKey("frames")) 
  24.     { 
  25.             printf("no dic\n");  
  26.     } 
  27.     for(int i=0;i<3;) 
  28.     { 
  29.         char picture[20]; 
  30.         sprintf(picture,"bg_%d.png",i); 
  31.         if(!rootDict.stepIntoSubDictWithKey(picture)) 
  32.         { 
  33.             i++; 
  34.             //printf("No    %s\n",picture); 
  35.             rootDict.stepOutOfSubDict(); 
  36.             continue
  37.         } 
  38.         int x=rootDict.getIntegerForKey("x"); 
  39.         int y=rootDict.getIntegerForKey("y"); 
  40.         int width=rootDict.getIntegerForKey("width"); 
  41.         int height=rootDict.getIntegerForKey("height"); 
  42.     } 
  43.     void stepBackToRootSubDict();