PDA

View Full Version : why don't the Children in my nsdictionary fill up




ulquiorra
Sep 4, 2009, 03:40 AM
Hello all I'm using NSXMLParser to parse some xml data. I'm using this data to build a Tree representation to create a hierarchy for a drilldowntable.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {

if([elementName isEqualToString:@"exercises"]) {
//init tree
appDelegate.Tree = [NSMutableDictionary new];
appDelegate.Rows = [NSMutableArray new];
[appDelegate.Tree setObject:appDelegate.Rows forKey:@"Rows"];
...



Now if the element menuitem in my xml is being read then it will initialize and add theChildren to the menu_item dictionary


if([elementName isEqualToString:@"menuitem"]) {


appDelegate.theChildren = [NSMutableArray new];
[appDelegate.menu_item setObject:appDelegate.theChildren forKey:@"Children"];



This is all going according to plan.

However the following is not going the way I want it to go. When I see another tag (workout_mob) I want theChildren to be filled and this only happens with the bottom dictionary item.

http://img188.imageshack.us/img188/7052/picture4efx.png


if([elementName isEqualToString:@"workout_mob"]) {

appDelegate.work_out_mob = [NSMutableDictionary new];

[appDelegate.theChildren addObject:appDelegate.work_out_mob];



Does anyone have a clue as to why it isn't at least filling both my items I mean it's adding the Children array to the both of them so why just the bottom one for the rest. If I add another xmlline in my xml code like so:


<menu_mob>
<menuitem id="1" text="Press this for exercises" pic="workout" />
<menuitem id="2" text="VirtuaGym online" pic="online" />
*<menuitem id="3" text="the Added Line" pic="online" />*
</menu_mob>


then it will add all the Children items to the bottom menuitem ( imagine another dictionary item (3)). So why ?



dejo
Sep 4, 2009, 09:00 AM
If menuitem is always a child node of workout_mob / menu_mob (not sure which since your code snippet uses workout_mob but your XML snippet uses menu_mob), then here is your problem:
if([elementName isEqualToString:@"menuitem"]) {
appDelegate.theChildren = [NSMutableArray new];
Every time you encounter a new child node (menuitem) you are resetting the array for the parent node (workout_mob).

ulquiorra
Sep 6, 2009, 01:09 PM
thanks man! that was it.... I was indeed resetting my array..