NSxmlparser data to array

Discussion in 'iOS Programming' started by ndv, Mar 12, 2011.

  1. ndv macrumors newbie

    Joined:
    Mar 12, 2011
    #1
    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 :)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    MeinaItemsArray is locally scoped to the method it is in and is a new array every time.
     
  3. ndv thread starter macrumors newbie

    Joined:
    Mar 12, 2011
    #3
    Thanks for the quick reply :)
    So what is the correct way to do this?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. ndv thread starter macrumors newbie

    Joined:
    Mar 12, 2011
    #5
    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?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. ulbador, Mar 12, 2011
    Last edited: Mar 12, 2011

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #7
    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?
     
  8. ndv thread starter macrumors newbie

    Joined:
    Mar 12, 2011
    #8
    ulbador - thank you, that the exact explanation i needed.

    thank you both for your time.
     

Share This Page