Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

XML Parsing not showing what expected array value

aceM3aceM3 In the End!Posts: 1New Users Noob
Im having an XML like this, and is parsing it and storing it in an Array of Dictionary using there element as a key.
<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>

the thing is it doesnt let me show what i intended on having, my expected array value should be like this
{
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;
}
);
}

But my actual value is like this, and the difference between my expected value is that the sub category value in the 2nd and 3rd category doesn't show, where their key is sub_cat an element from the XML
{
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;
}
}

and my parser code is like this,
- (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;

}


}

I just want to ask what I'm missing here and why is it not showing what i wanted to be shown .

Replies

Sign In or Register to comment.