您可以使用XMLReader文件将 xml 数据解析为 NSDictionary 或 NSArray 或 MutableArray。将其用作:
NSError *error=nil;
NSString *responseString=[NSString stringWithFormat:@"<xml><Menu><pair><MenuCategory shortname=\"Starters Menu\" description_en=\"hgfhghgh\" image=\"Small.jpg\" description_fr=\"\" description_ge=\"\" description_it=\"\" description_sp=\"\" description_ch=\"\"><MenuItems><menuItem price=\"666\" image=\"060649XSmall.jpg\" shortname=\"kandan\" description_en=\"kandan\" description_fr=\"\" description_ge=\"\" description_it=\"\" description_sp=\"\" description_ch=\"\"/><menuItem price=\"250\" image=\"3.jpg\" shortname=\"Vegetable Soups\" description_en=\"Vegetable Soups\" description_fr=\"\" description_ge=\"\" description_it=\"\" description_sp=\"\" description_ch=\"\"/><menuItem price=\"52\" image=\"3.jpg\" shortname=\"Mixed Starters\" description_en=\"Mixed Starters\" description_fr=\"\" description_ge=\"\" description_it=\"\" description_sp=\"\" description_ch=\"\"/><menuItem price=\"45\" image=\"14.jpg\" shortname=\"Pumpkin soup\" description_en=\"Pumpkin soup\" description_fr=\"\" description_ge=\"\" description_it=\"\" description_sp=\"\" description_ch=\"\"/><menuItem price=\"15\" image=\"15.jpg\" shortname=\"Almondrolledgoatsche\" description_en=\"Almondrolledgoatsche\" description_fr=\"\" description_ge=\"\" description_it=\"\" description_sp=\"\" description_ch=\"\"/></MenuItems></MenuCategory></pair></Menu></xml>"];
NSDictionary *dictionary=[XMLReader dictionaryForXMLString:responseString error:&error];
NSLog(@"dictioanry is %@",dictionary);
NSMutableArray *menuItemsArray=[[NSMutableArray alloc] initWithArray:[[[[[[dictionary valueForKey:@"xml"] valueForKey:@"Menu"] valueForKey:@"pair"] valueForKey:@"MenuCategory"] valueForKey:@"MenuItems"] valueForKey:@"menuItem"]];
NSLog(@"menu item array is %@",menuItemsArray);
控制台日志:--------
dictioanry is {
xml = {
Menu = {
pair = {
MenuCategory = {
MenuItems = {
menuItem = (
{
"description_ch" = "";
"description_en" = kandan;
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "060649XSmall.jpg";
price = 666;
shortname = kandan;
},
{
"description_ch" = "";
"description_en" = "Vegetable Soups";
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "3.jpg";
price = 250;
shortname = "Vegetable Soups";
},
{
"description_ch" = "";
"description_en" = "Mixed Starters";
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "3.jpg";
price = 52;
shortname = "Mixed Starters";
},
{
"description_ch" = "";
"description_en" = "Pumpkin soup";
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "14.jpg";
price = 45;
shortname = "Pumpkin soup";
},
{
"description_ch" = "";
"description_en" = Almondrolledgoatsche;
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "15.jpg";
price = 15;
shortname = Almondrolledgoatsche;
}
);
};
"description_ch" = "";
"description_en" = hgfhghgh;
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "Small.jpg";
shortname = "Starters Menu";
};
};
};
};
}
2013-03-06 11:52:31.500 MobileTrading[1212:11303] menu item array is (
{
"description_ch" = "";
"description_en" = kandan;
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "060649XSmall.jpg";
price = 666;
shortname = kandan;
},
{
"description_ch" = "";
"description_en" = "Vegetable Soups";
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "3.jpg";
price = 250;
shortname = "Vegetable Soups";
},
{
"description_ch" = "";
"description_en" = "Mixed Starters";
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "3.jpg";
price = 52;
shortname = "Mixed Starters";
},
{
"description_ch" = "";
"description_en" = "Pumpkin soup";
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "14.jpg";
price = 45;
shortname = "Pumpkin soup";
},
{
"description_ch" = "";
"description_en" = Almondrolledgoatsche;
"description_fr" = "";
"description_ge" = "";
"description_it" = "";
"description_sp" = "";
image = "15.jpg";
price = 15;
shortname = Almondrolledgoatsche;
}
)