0

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So how do this, i cant really seem to find the solution?
@user3195388: Call lists = [[NSMutableArray alloc]init]; lists2 = ...; just before you call the parse method of NSXMLParser, and not in didStartElement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.