I have a XML where it will represent a product catalog, in which each product belongs to a category. ie:
<Product category="ABC">
<Item_number>123</Item_number>
</Product>
<Product category="ABC">
<Item_number>456</Item_number>
</Product>
<Product category="XYZ">
<Item_number>789</Item_number>
</Product>
I have created a class to store the data, and then put an instance into an array.* Then displaying it onto a tableview .. Currently, tableview shows a list of Item_numbers 😀
What i’m trying to achieve is the same, but I wish to have 2 pages. The main tableview with non repeating (unique) category (ie. ABC, XYZ), and on the 2nd page – detail tableview it will display the item_number that belongs to the category selected. like this ..
Main Tableview:
ABC >
XYZ >
Detail Tableview (selected “ABC”):
123
456
(1) What’s the best approach for this?
(2) I guess my 1st obstacle is parsing the value out from the category “ABC”
<Product category="ABC">
in my implementation I’m getting blanks when I simply call (somehow parsing a value when there’s a category in the XML is a bit different)
cell.textLabel.text = product.product;
(3) The 2nd is how can I store the data (or can I keep the same approach*), where the detail Tableview page can be referenced by the user selecting a single category from the 1st page.
Current implementation for didStartElement
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:@"Product"] ){
self.currentProduct = [[Product alloc] init];
self.storeCharacters = NO;
} else if ([elementName isEqualToString:@"Item_Number"]) {
[self.currentString setString:@""];
self.storeCharacters = YES;
}
}
Regarding parsing the
category, that’s retrieved fromdidStartElementfrom theattributesdictionary whenelementNameis@"Product".In terms of the “best approach”, you just need to decide on your data structure. I might suggest building a NSMutableArray which is an array of category dictionary entries, one per category, and one of the objects in the dictionary would be an array of products in that category.
Once you have that structure, answering your third question is probably obvious.
Ok, first, your XML really needs a outer tag, something like:
Second, assuming you wanted an array of categories, and for each category, you wanted an array of products, the implementation might look like the following. First you’d want a few properties, one for the final result and two more for temporary variables used during parsing:
And then the
NSXMLParserDelegatemethods might look like: