0

I would like to pass the nsdictionary I am creating into an nsmutablearray but I'm not sure when or how to do it in the nsxmlparser delegates.

this is what I have done so far

#pragma mark - Parsing lifecycle

- (void)startTheParsingProcess:(NSData *)parserData
{    
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process 

    [parser setDelegate:self];
    [parser parse]; // starts the event-driven parsing operation.
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
{
    if ([elementName isEqualToString:@"item"]) {
        valueDictionary = [[NSMutableDictionary alloc] init];
    }    
}

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSMutableString *dicString = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
    currentElement = dicString;
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
    if ([elementName isEqualToString:@"title"]) {
        titleString = currentElement;
        [self.valueDictionary setObject:titleString forKey:@"title"];

        NSLog(@"%@", [valueDictionary objectForKey:@"title"]);
        NSLog(@" ");
        currentElement = nil;
    }
    if ([elementName isEqualToString:@"description"]) 
    {
        descriptionString = currentElement;
        [self.valueDictionary setObject:descriptionString forKey:@"description"];

        NSLog(@"%@", [valueDictionary objectForKey:@"description"]);
        NSLog(@" ");
        currentElement = nil;
    }
C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • 1
    Lots of memory leaks in your code, make sure to release values before reassigning them to objects you take ownership of, and also release them in dealloc. Setting them to nil is not enough. – Joe Dec 21 '11 at 21:52
  • I am very confused about releasing things since ARC was introduced.. I will investigate.. but I'm not 100% on how to do this sucsessfully. – C.Johns Dec 21 '11 at 21:57
  • Well then I take that back if you are using ARC. Carry On. – Joe Dec 21 '11 at 22:03
  • lol :P I have a few vids from WWDC to watch on arc.. I have done heaps of reading etc and I think I have most of it down.. but there are a few things I need to get my head around.. thanks for the comment tho – C.Johns Dec 21 '11 at 22:09

1 Answers1

2

In -parser:didEndElement:namespaceURI:qualifiedName:, listen for the end of the item element, then add valueDictionary to a mutable array instance on your class.

if ([elementName isEqualToString:@"item"])
{
    [self.mutableArrayOfDictionaries addObject:self.valueDictionary];
}

if ([elementName isEqualToString:@"title"]) {
    titleString = currentElement;
    [self.valueDictionary setObject:titleString forKey:@"title"];

    NSLog(@"%@", [valueDictionary objectForKey:@"title"]);
    NSLog(@" ");
    currentElement = nil;
}

if ([elementName isEqualToString:@"description"]) 
{
    descriptionString = currentElement;
    [self.valueDictionary setObject:descriptionString forKey:@"description"];

    NSLog(@"%@", [valueDictionary objectForKey:@"description"]);
    NSLog(@" ");
    currentElement = nil;
}
Mark Adams
  • 30,776
  • 11
  • 77
  • 77