Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ndv

macrumors newbie
Original poster
Mar 12, 2011
4
0
Hi all,
I have a little question regarding xml data and arrays.
So i'm parsing an xml file and i'm trying to add the parsed data into an array which i can later use as the cell data for a table view.

i keep getting the last node from the xml and i don't understand why.

here's the parser code:

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName compare:@"ArticleTitle"])
	{
		currentAttribute = [NSMutableString string];
        
	}

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if(![elementName compare:@"ArticleTitle"])
	{
 
        MeinaItemsArray = [[NSMutableArray alloc] initWithObjects:currentAttribute, nil];
        
        NSLog(@"The is %@", MeinaItemsArray);
	}

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if(self.currentAttribute)
	{
		[self.currentAttribute appendString:string];
	}
}


Thanks in advance :)
 
Thanks for the quick reply :)
So what is the correct way to do this?
 
Thanks for the quick reply :)
So what is the correct way to do this?

Have an array that is in scope for all the calls and add the items you want to that. If you are looking for code I am not about to provide that: I firmly believe the only way to learn is to write the code yourself.
 
I'm not looking for you to write my code, i'm just looking for the correct way to achieve what i'm trying to do.

So you're saying i need to have my array in a different place and addObjects to it?
 
I'm not looking for you to write my code, i'm just looking for the correct way to achieve what i'm trying to do.

So you're saying i need to have my array in a different place and addObjects to it?

This question tells me you don't understand the very basics. Stop writing code. Ensure you understand variable scoping. This is basic C. Not even Objective-C.
 
MeinaItemsArray is locally scoped to the method it is in and is a new array every time.

This is exactly what you need to know.

Every time you try to add a new XML element to your array, you overwrite the WHOLE thing.

This means when you are done, the only thing in your array is the very last XML element that you added. This has got to be leaking memory too doesn't it?
 
Last edited:
ulbador - thank you, that the exact explanation i needed.

thank you both for your time.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.