Search code examples
iphoneobjective-crss

Parse 2 RSS feeds in app delegate


I'm following a tutorial and it's working fine but if I wanted to parse 2 RSS feeds it seems to overwrite one array instead of saving them in the respective arrays.

This is in my delegate:

NSURL *url2 = [[NSURL alloc] initWithString:@"myRSSFEED1"];
NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithContentsOfURL:url2];

//Initialize the delegate.
XMLParser1 *parser1 = [[XMLParser1 alloc] initXMLParser];

//Set delegate
[xmlParser1 setDelegate:parser1];

//Start parsing the XML file.
BOOL successs = [xmlParser1 parse];

if(successs)
    NSLog(@"No Errors");
else
    NSLog(@"Error Error Error!!!");
 //VIDS
NSURL *url = [[NSURL alloc] initWithString:@"MYRSSFEED2"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate
[xmlParser setDelegate:parser];

//Start parsing the XML file.
BOOL success = [xmlParser parse];

if(success)
    NSLog(@"No Errors");
else
    NSLog(@"Error Error Error!!!"); 

that parses feed 1, allocates it to the array then parses 2 and seems to overwrite the first insteaf of using the second array that are defined as

@synthesize pics;
@synthesize books;

And saved in my XMLParser & XMLParser1

I can't figure out how to stop it from overwriting.

Here is my XMLParsers too:

   - (void)parsers:(NSXMLParser *)parsers didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"Books"]) {
        //Initialize the array.
        appDelegate2.pics = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"]) {

        //Initialize the book.
        apics = [[BookPhoto alloc] init];

        //Extract the attribute here.
        apics.bookID = [[attributeDict objectForKey:@"id"] integerValue];

        NSLog(@"Reading HAVid value :%i", apics.bookID);
    }

    NSLog(@"Processing Element: %@", elementName);
}

- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

- (void)parsers:(NSXMLParser *)parsers didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Books"])

        return;

    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"Book"]) {
        [appDelegate2.pics addObject:apics];


        [apics release];
        apics = nil;
    }
    else 
        [apics setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
}

And my XMLParser.m

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"Books"]) {
        //Initialize the array.
        appDelegate.books = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"]) {

        //Initialize the book.
        aBook = [[Book alloc] init];

        //Extract the attribute here.
        aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];

        NSLog(@"Reading id value :%i", aBook.bookID);
    }

    NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Books"])
        return;

    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"Book"]) {
        [appDelegate.books addObject:aBook];

        [aBook release];
        aBook = nil;
    }
    else 
        [aBook setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
}

Any help on this would be brilliant,


Solution

  • my bad

    seems i had extra s's in parser:

    - (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string { 
    
        if(!currentElementValue) 
            currentElementValue = [[NSMutableString alloc] initWithString:string];
        else
            [currentElementValue appendString:string];
    
        NSLog(@"Processing Value: %@", currentElementValue);
    
    }
    

    should have been:

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    
        if(!currentElementValue) 
            currentElementValue = [[NSMutableString alloc] initWithString:string];
        else
            [currentElementValue appendString:string];
    
        NSLog(@"Processing Value: %@", currentElementValue);
    
    }
    

    cheers for the help tho