MWFeedParser icon indicating copy to clipboard operation
MWFeedParser copied to clipboard

How display image for different RSSfedd's Key. like (media:content , image, thumbnail)

Open badalpub1991 opened this issue 9 years ago • 0 comments

I am working on RSSFeed project. i want to fetch data from five different url's XML.

  1. http://rss.news.yahoo.com/rss/world - Yahoo.com

  2. http://news.google.com/news?pz=1&ned=us&hl=en&topic=w&output=rss - Google.com

  3. http://rssfeeds.usatoday.com/UsatodaycomWorld-TopStories - USA Today

I am able to Fetch data like Title and Description or etc with

Object for key @"Ttile" and Object for key "@PUBDATE"

But in Terms of Image all RSS Feed using Different Key for image Like Object for key @"image" , Object for key @"src" , Object for key @"Media:content"

I am able to fetch few of the Image with Objectforkey:@"media:content" but other link uses different Objectforkey like @"image" or @"Media:thumbline".

How can i fetch it this different key or how can i set it in single tableview?

I am Using MWFeedParser for XML Parsing.

Here is my code from which i am able to fetch image with objectforkey:@"Media:content" but i can't understand how to set other keys for image.

    //MWFeeditem.h
#import <Foundation/Foundation.h>
@interface MWFeedItem : NSObject <NSCoding> {

NSString *identifier; // Item identifier
NSString *title; // Item title
NSString *link; // Item URL
NSDate *date; // Date the item was published
NSDate *updated; // Date the item was updated if available
NSString *summary; // Description of item
NSString *content; // More detailed content (if available)
NSString *author; // Item author
NSString *image; // image for the feed

// Enclosures: Holds 1 or more item enclosures (i.e. podcasts, mp3. pdf, etc)
//  - NSArray of NSDictionaries with the following keys:
//     url: where the enclosure is located (NSString)
//     length: how big it is in bytes (NSNumber)
//     type: what its type is, a standard MIME type  (NSString)
NSArray *enclosures;

}

@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *link;
@property (nonatomic, copy) NSDate *date;
@property (nonatomic, copy) NSDate *updated;
@property (nonatomic, copy) NSString *summary;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *author;
@property (nonatomic, copy) NSArray *enclosures;
@property (nonatomic, copy) NSString *image;
@end

// MWFeeditem.m

#pragma mark NSCoding

 - (id)initWithCoder:(NSCoder *)decoder {
 if ((self = [super init])) {
    identifier = [decoder decodeObjectForKey:@"identifier"];
    title = [decoder decodeObjectForKey:@"title"];
    link = [decoder decodeObjectForKey:@"link"];
    date = [decoder decodeObjectForKey:@"date"];
    updated = [decoder decodeObjectForKey:@"updated"];
    summary = [decoder decodeObjectForKey:@"summary"];
    content = [decoder decodeObjectForKey:@"content"];
    author = [decoder decodeObjectForKey:@"author"];
    enclosures = [decoder decodeObjectForKey:@"enclosures"];
    image = [decoder decodeObjectForKey:@"media:content"];

       }
 return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
if (identifier) [encoder encodeObject:identifier forKey:@"identifier"];
if (title) [encoder encodeObject:title forKey:@"title"];
if (link) [encoder encodeObject:link forKey:@"link"];
if (date) [encoder encodeObject:date forKey:@"date"];
if (updated) [encoder encodeObject:updated forKey:@"updated"];
if (summary) [encoder encodeObject:summary forKey:@"summary"];
if (content) [encoder encodeObject:content forKey:@"content"];
if (author) [encoder encodeObject:author forKey:@"author"];
if (enclosures) [encoder encodeObject:enclosures forKey:@"enclosures"];
if (image) [encoder encodeObject:image forKey:@"media:content"];

}

@end

// MWFwwsparser.m

   - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
MWXMLLog(@"NSXMLParser: didEndElement: %@", qName);
@autoreleasepool {

    // Parse content as structure (Atom feeds with element type="xhtml")
    // - Use elementName not qualifiedName to ignore XML namespaces for XHTML entities
    if (parseStructureAsContent) {

        // Check for finishing parsing structure as content
        if (currentPath.length > pathOfElementWithXHTMLType.length) {

            // Close XHTML tag unless it is an empty element
            if (!ELEMENT_IS_EMPTY(elementName)) [currentText appendFormat:@"</%@>", elementName];

            // Adjust path & don't continue
            self.currentPath = [currentPath stringByDeletingLastPathComponent];

            // Return
            return;

        }

        // Finish
        parseStructureAsContent = NO;
        self.pathOfElementWithXHTMLType = nil;

        // Continue...

    }

    // Store data
    BOOL processed = NO;
    if (currentText) {

        // Remove newlines and whitespace from currentText
        NSString *processedText = [currentText stringByRemovingNewLinesAndWhitespace];

        // Process
        switch (feedType) {
            case FeedTypeRSS: {

                // Specifications
                // http://cyber.law.harvard.edu/rss/index.html
                // http://web.resource.org/rss/1.0/modules/dc/ Dublin core also seems to be used for RSS 2 as well

                // Item
                if (!processed) {
                    if ([currentPath isEqualToString:@"/rss/channel/item/title"]) { if (processedText.length > 0) item.title = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/link"]) { if (processedText.length > 0) item.link = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/author"]) { if (processedText.length > 0) item.author = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/dc:creator"]) { if (processedText.length > 0) item.author = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/guid"]) { if (processedText.length > 0) item.identifier = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/description"]) { if (processedText.length > 0) item.summary = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/content:encoded"]) { if (processedText.length > 0) item.content = processedText; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/pubDate"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822]; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
                    else if ([currentPath isEqualToString:@"/rss/channel/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }

           ***// Changes Done here for image //***
                    else if ([currentPath isEqualToString:@"/rss/channel/item/media:content"])
                    {
                        if ([self.currentElementAttributes objectForKey:@"url"])
                            item.image = self.currentElementAttributes[@"url"];
                                                    processed =    YES;      }



                }

and here is my UITableview Method where i have to set image in cell.

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *MainTableIdentifier = @"MainTableIdentifier";
     MainViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MainTableIdentifier];

 if (cell == nil)
  {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MainViewTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
   }


MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {

    // Process
    NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
    NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]";


    cell.lblTitle.text = itemTitle;
    NSMutableString *subtitle = [NSMutableString string];
    if (item.date) [subtitle appendFormat:@"%@ ", [formatter stringFromDate:item.date]];
    //[subtitle appendString:itemSummary];
    cell.lblDateTime.text = subtitle;
    cell.lblDescription.text=itemSummary;


}
NSURL *url = [NSURL URLWithString:item.image];

   [cell.ImgMainView sd_setImageWithURL:url];

For More Reference Here i have Attached Pastie.org link with full code.

MWFeed parsers's class file with changes

MWFeedItem.h

MWFeedItem.m

MWFeedParser.h

MWFeedParser.m

Application's class file .

MainViewController.h

MainViewController.m

badalpub1991 avatar Aug 27 '15 09:08 badalpub1991