2011-08-19 40 views
1

我'能夠使用這個代碼NSDictionary的轉換爲XML格式在iphone

NSString *testXMLString = @"<items><item id=\"0001\" type=\"donut\"><name>Cake</name><ppu>0.55</ppu><batters><batter id=\"1001\">Regular</batter><batter id=\"1002\">Chocolate</batter><batter id=\"1003\">Blueberry</batter></batters><topping id=\"5001\">None</topping><topping id=\"5002\">Glazed</topping><topping id=\"5005\">Sugar</topping></item></items>"; 
    NSError *parseError = nil; 
    NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError]; 
    NSLog(@"%@", xmlDictionary); 

顯示在NSDictionary中的數據,並輸出

items =  { 
     item =   { 
      batters =    { 
       batter =     (
             { 
         id = 1001; 
         text = Regular; 
        }, 
             { 
         id = 1002; 
         text = Chocolate; 
        }, 
             { 
         id = 1003; 
         text = Blueberry; 
        } 
       ); 
      }; 
      id = 0001; 
      name =    { 
       text = Cake; 
      }; 
      ppu =    { 
       text = "0.55"; 
      }; 
      topping =    (
           { 
        id = 5001; 
        text = None; 
       }, 
           { 
        id = 5002; 
        text = Glazed; 
       }, 
           { 
        id = 5005; 
        text = Sugar; 
       } 
      ); 
      type = donut; 
     }; 
    }; } 

任何人都可以請幫我如何轉換這字典到一個XML文件?

回答

0

NSPropertyListSerialization類應該能夠爲您做到這一點。

+0

可以ü請給我示例代碼我是新的目標c – user564963