先要做准备工作

先下载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. <!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>texture</key> 
  6.     <dict> 
  7.         <key>width</key> 
  8.         <integer>512</integer> 
  9.         <key>height</key> 
  10.         <integer>1024</integer> 
  11.     </dict> 
  12.     <key>frames</key> 
  13.     <dict> 
  14.         <key>bg_0.png</key> 
  15.         <dict> 
  16.             <key>x</key> 
  17.             <integer>1</integer> 
  18.             <key>y</key> 
  19.             <integer>289</integer> 
  20.             <key>width</key> 
  21.             <integer>480</integer> 
  22.             <key>height</key> 
  23.             <integer>287</integer> 
  24.             <key>offsetX</key> 
  25.             <real>0</real> 
  26.             <key>offsetY</key> 
  27.             <real>1</real> 
  28.             <key>originalWidth</key> 
  29.             <integer>480</integer> 
  30.             <key>originalHeight</key> 
  31.             <integer>289</integer> 
  32.         </dict> 
  33.         <key>bg_2.png</key> 
  34.         <dict> 
  35.             <key>x</key> 
  36.             <integer>1</integer> 
  37.             <key>y</key> 
  38.             <integer>1</integer> 
  39.             <key>width</key> 
  40.             <integer>480</integer> 
  41.             <key>height</key> 
  42.             <integer>287</integer> 
  43.             <key>offsetX</key> 
  44.             <real>0</real> 
  45.             <key>offsetY</key> 
  46.             <real>0</real> 
  47.             <key>originalWidth</key> 
  48.             <integer>480</integer> 
  49.             <key>originalHeight</key> 
  50.             <integer>287</integer> 
  51.         </dict> 
  52.         <key>bg_1.png</key> 
  53.         <dict> 
  54.             <key>x</key> 
  55.             <integer>1</integer> 
  56.             <key>y</key> 
  57.             <integer>577</integer> 
  58.             <key>width</key> 
  59.             <integer>480</integer> 
  60.             <key>height</key> 
  61.             <integer>287</integer> 
  62.             <key>offsetX</key> 
  63.             <real>0</real> 
  64.             <key>offsetY</key> 
  65.             <real>0</real> 
  66.             <key>originalWidth</key> 
  67.             <integer>480</integer> 
  68.             <key>originalHeight</key> 
  69.             <integer>287</integer> 
  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();