我有这个简单的 XML 并正在解析它并将其子数据传递到另一个视图控制器,我的 XML 是这样的,
<categories>
<category>
<name>Electronics</name>
<description>This is the given sample description of main menu</description>
<image>Link Here</image>
<sub_cat>
<sub_name>Laptop</sub_name>
<sub_desc>sub cat description of Laptop</sub_desc>
<sub_image>Link Here</sub_image>
</sub_cat>
<sub_cat>
<sub_name>Printers</sub_name>
<sub_desc>sub cat description of Printers</sub_desc>
<sub_image>Link Here</sub_image>
</sub_cat>
</category>
<category>
<name>Food</name>
<description>This is the given sample description of main menu</description>
<image>Link Here</image>
<sub_cat>
<sub_name>Pizza</sub_name>
<sub_desc>sub cat description of pizza</sub_desc>
<sub_image>Link Here</sub_image>
</sub_cat>
<sub_cat>
<sub_name>Burgers</sub_name>
<sub_desc>sub cat description of Burgers</sub_desc>
<sub_image>Link Here</sub_image>
</sub_cat>
</category>
<category>
<name>Gifts</name>
<description>This is the given sample description of main menu</description>
<image>Link Here</image>
<sub_cat>
<sub_name>Photo Albums</sub_name>
<sub_desc>sub cat description of Photo Album</sub_desc>
<sub_image>Link Here</sub_image>
</sub_cat>
<sub_cat>
<sub_name>Car</sub_name>
<sub_desc>sub cat description of Car</sub_desc>
<sub_image>Link Here</sub_image>
</sub_cat>
</category>
</categories>
我使用 NSXMLParser 解析这个数组作为它的一个简单的 XML,我像这样解析它,
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"category"]){
_mainCategory = [[NSMutableDictionary alloc]init];
} else if([elementName isEqualToString:@"sub_cat"]){
_subCategory = [[NSMutableDictionary alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
currentData = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"name"]) {
[_mainCategory setValue:currentData forKey:elementName];
}
if ([elementName isEqualToString:@"description"]){
[_mainCategory setValue:currentData forKey:elementName];
}
if ([elementName isEqualToString:@"image"]) {
[_mainCategory setValue:currentData forKey:elementName];
}
if ([elementName isEqualToString:@"sub_name"]) {
[_subCategory setValue:currentData forKey:elementName];
}
if ([elementName isEqualToString:@"sub_desc"]) {
[_subCategory setValue:currentData forKey:elementName];
}
if ([elementName isEqualToString:@"sub_cat"]) {
[_childPopulated addObject:_subCategory];
}
if([elementName isEqualToString:@"category"]){
[_mainCategory setValue:_childPopulated forKey:@"sub_cat"];
[_listPopulated addObject:_mainCategory];
_mainCategory = nil;
_childPopulated = nil;
}
}
我的输出数据看起来像这样,
{
description = "This is the given sample description of main menu";
image = "Link Here";
name = Electronics;
"sub_cat" = (
{
"sub_desc" = "sub cat description of Laptop";
"sub_name" = Laptop;
},
{
"sub_desc" = "sub cat description of Printers";
"sub_name" = Printers;
}
);
},
{
description = "This is the given sample description of main menu";
image = "Link Here";
name = Food;
},
{
description = "This is the given sample description of main menu";
image = "Link Here";
name = Gifts;
}
但我的预期数据应该像该数组中的第一个索引,以 sub_cat 作为键。但是当它进入第二个子类别时,它没有获取另一个 sub_cat,
我的期望值应该是这样的,
{
description = "This is the given sample description of main menu";
image = "Link Here";
name = Electronics;
"sub_cat" = (
{
"sub_desc" = "sub cat description of Laptop";
"sub_name" = Laptop;
},
{
"sub_desc" = "sub cat description of Printers";
"sub_name" = Printers;
}
);
description = "This is the given sample description of main menu";
image = "Link Here";
name = Food;
"sub_cat" = (
{
"sub_desc" = "sub cat description of Laptop";
"sub_name" = Pizza;
},
{
"sub_desc" = "sub cat description of Printers";
"sub_name" = Burger;
}
);
description = "This is the given sample description of main menu";
image = "Link Here";
name = Gift;
"sub_cat" = (
{
"sub_desc" = "sub cat description of Laptop";
"sub_name" = Car;
},
{
"sub_desc" = "sub cat description of Printers";
"sub_name" = Photo Album;
}
);
}
我不知道我在解析这个时缺少什么,只是想知道为什么其余的子类别没有出现。