views:

32

answers:

2

Hi,

how can I extract the titel of a RSS channel while not getting in conflict with the title element of a news item?

If an element was found:

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

   if ([elementName isEqualToString:@"channel") {
       [currentChannel release];
       currentChannel = nil;

       currentChannel = [[NSMutableString alloc] init];
   }

   if ([elementName isEqualToString:@"item"]) {
      ...
      }
   }

If the end-tag was found:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
   if ([elementName isEqualToString:@"channel") {
       [channel setObject:currentChannelTitle forKey:@"title"];
   }

   if ([elementName isEqualToString:@"item"]) {
       ...
   }

Do the parsing stuff:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
   string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
   if ([currentElement isEqualToString:@"title") {
        [currentChannelTitle appendString:string forKey:@"title"];
   }

   if ([currentElement isEqualToString:@"title"]) {
        [currentTitle appendString:string];
   }
    ...
   }

And in the last part I have the problem. I have two "title" attributes. One for the "channel" element and one for the child element of it ("item"). But I need a distinction somehow. But how?

A: 

i think you want to get item title, in that case in viewDidload method take one bool type variable say flag in viewDidload() method set flag=NO;

when during parsing you find a start element as "Item" set flag = YES, and In parser didendElement method when you found element as "Title", check that flag=YES or not. store only if it is YES. and also in ParserDidEndElement when you find element as "Item" again set flag=NO;

Matrix
testing
A: 

I found another example to support different categories of the feed and I come up with the following solution:

First declare the variable in the *.h file.

@interface XMLParser : NSObject <NSXMLParserDelegate>{
    ...  
    NSMutableString * currentChannel;
}

In the *.m file synthesize and release the variable. In didStartElement ask which element you are currently working on. If it is a channel element, extract the title and store it in the above declared variable.

@implementation XMLParser

...
@synthesize currentChannel;

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

if ([elementName isEqualToString:@"item"]) {  
    ...
}

// title is an attribute of the element channel
if ([currentElement isEqualToString:@"channel"]) {
    [currentChannel appendString:[attributeDict objectForKey:@"title"]];
}  

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {  
    ...

    if ([currentChannel isEqualToString:@"Your Channel Name"]) {
        if ([currentCategory isEqualToString:@"Your category name"]) {
            [items addObject:[item copy]];
        }
    } else {
        [items addObject:[item copy]];
    }
}  
}  

In didEndElement ask if the item belongs to a specific channel, and if then add it only if it is the defined category. So you can add/show only rss feeds, if they belong to a certain category.

So i've not tested it, because the channel only have one category. So I don't have to ask for the channel titel, to show only defined categories from a specific channel. But I think that should work.

testing