i'm trying to parse an xml document into 2 NSMutableArrays, but it seems like it is overwriting my array. my xml doc contain 3 objects, but my array only contain 1. Why does it overwrite the previous added objects from the xml doc?
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"playlist"]) {
title = [attributeDict valueForKey:@"title"];
listid = [[attributeDict valueForKey:@"id"] intValue];
lists = [[NSMutableArray alloc]init];
lists2 = [[NSMutableArray alloc]init];
[lists addObject:title];
[lists2 addObject:[NSString stringWithFormat:@"%d", listid]];
}
NSLog(@"%d", lists.count);
}
xml doc:
<?xml version="1.0"?>
<Lists>
<playlist title="Quo Vadis" id="0"/>
<playlist title="Lord of the Rings" id="1"/>
<playlist title="Face" id="2"/>
</Lists>
1 Answer 1
You assign a new (empty) array to lists and lists2 each time in didStartElement,
and therefore overwrite what was previously in those variables.
The arrays must be created only once, before you start the parse method.
answered Jan 21, 2014 at 20:25
Martin R
541k98 gold badges1.3k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
user3195388
So how do this, i cant really seem to find the solution?
Martin R
@user3195388: Call
lists = [[NSMutableArray alloc]init]; lists2 = ...; just before you call the parse method of NSXMLParser, and not in didStartElement.default