IOS XMLParsing。如何为同一对象加载不同的键

时间:2015-08-24 11:50:45

标签: ios xml-parsing

我正在研究RSSFeed项目。我想从五个不同的网址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 - 今日美国

我可以使用

获取标题和描述等数据

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

但是在图像方面,所有使用不同键的RSS Feed都适用于图像 Object for key @"image"Object for key @"src"Object for key @"Media:content"

我可以使用Objectforkey:@"media:content"获取少量图片 但其他链接使用不同的Objectforkey,如@" image"或@"媒体:缩略图"。

我如何获取这个不同的密钥或如何在单个tableview中设置它?

我正在使用MWFeedParser进行XML解析。

以下是我可以使用objectforkey:@"Media:content"获取图片的代码,但我无法理解如何为图片设置其他按键。

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



                }

这是我的UITableview方法,我必须在单元格中设置图像。

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

更多参考资料在这里,我附上了Pastie.org链接的完整代码。

MWFeed解析器的类文件包含更改

MWFeedItem.h

MWFeedItem.m

MWFeedParser.h

MWFeedParser.m

应用程序的类文件。

MainViewController.h

MainViewController.m

0 个答案:

没有答案