how can I extract the title 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?
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.
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.
}
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.