2011-08-19 86 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