在app delegate中解析2个RSS提要

时间:2011-07-13 23:37:18

标签: iphone objective-c rss

我正在按照教程进行操作,但是如果我想解析2个RSS源,它似乎会覆盖一个数组而不是将它们保存在相应的数组中。

这是我的代表:

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!!!"); 

解析feed 1,将其分配给数组然后解析2并且似乎覆盖使用定义为

的第二个数组的第一个insteaf
@synthesize pics;
@synthesize books;

并保存在我的XMLParser& XMLParser1

我无法弄清楚如何阻止它被覆盖。

这也是我的XMLParsers:

   - (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;
}

我的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;
}

对此的任何帮助都很棒,

2 个答案:

答案 0 :(得分:0)

我建议在没有加载数组的位置放置断点,并查看是否(1)它甚至被调用,以及(2)是否将数据保存到数组中。

答案 1 :(得分:0)

我的不好

似乎我在解析器中有额外的s:

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

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

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

}

应该是:

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

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

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

}

欢呼帮助