简体   繁体   English

用目标C解析RSS

[英]Parsing RSS with Objective C

I am trying to parse XML file of RSS News. 我正在尝试解析RSS新闻的XML文件。 First, I tried the RSS from http://ria.ru/export/rss2/index.xml and everything worked perfectly. 首先,我尝试了来自http://ria.ru/export/rss2/index.xml的RSS,一切正常。 Then I tried another resource, namely http://interfax.ru/rss.asp and I encountered a problem with the date: 然后,我尝试了另一个资源,即http://interfax.ru/rss.asp ,但我遇到了日期问题:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];

NSDate *dateFormatFromString = [dateFormat dateFromString:[item objectForKey:@"Date"]]; 

NSLog(@"%@", [item objectForKey:@"Date"]);

NSDateFormatter *dateFormatNew = [[NSDateFormatter alloc]init];
[dateFormatNew setDateFormat:@"h:mm a, MMM d, YYYY"];    
NSString *stringTime = [dateFormatNew stringFromDate:dateFormatFromString];

NSLog(@"%@", stringTime);

[item objectForKey:@"Date"] does not have a problem, it's the same thing as in the RSS. [item objectForKey:@"Date"]没有问题,与RSS中的问题相同。 Foor example: 主要示例:

Wed, 25 Jan 2012 16:41:00 +0400

However, the second RSS with same date format gives me NULL . 但是,具有相同日期格式的第二个RSS给我NULL Both stringTime and dateFormatFromString produces NULL values as well. stringTimedateFormatFromString stringTime产生NULL值。 I changed the address RSS with the same format date and still have this problem. 我更改了具有相同格式日期的地址RSS,但仍然遇到此问题。

If you are trying to make an RSS reader for any feed (not just a specific feed that you own), you need to deal with all kinds of malformed date formats. 如果您要为任何提要(不仅仅是您拥有的特定提要)制作RSS阅读器,则需要处理各种格式错误的日期格式。 Even though there are specifications, many RSS feeds don't follow them. 即使有规范,许多RSS feed也没有遵循它们。

One approach is to have an array of date formats, and enumerate through it until a non-nil string is returned: 一种方法是拥有一个日期格式数组,并枚举它直到返回一个非null字符串:

static NSString *sGetDateForString(NSString *inString)
{
     static NSArray *sPossibleDateFormats = nil;

     if (!sPossibleDateFormats) sPossibleDateFormats = [[NSArray alloc] initWithObjects:
         @"EEE, d MMM yyyy HH:mm:ss Z",
         @"h:mm a, MMM d, YYYY",
         // Add more formats here as you encounter them in the wild
         nil];

     NSDate *result = nil;

     for (NSString *format in sPossibleDateFormats) {
         NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
         [dateFormat setDateFormat:format];

         result = [dateFormat dateFromString:dateString]; 

         [dateFormat release];

         if (result) break;
     }

     return result;
 }

This is going to be slow, but it may be fast enough for your app. 这将很慢,但对于您的应用来说可能足够快。 If you need additional performance, you can try caching the index of the matched date format and pass it back into sGetDateForString() (most RSS feeds will only use one date format). 如果需要其他性能,则可以尝试缓存匹配日期格式的索引,然后将其传递回sGetDateForString()(大多数RSS feed都将仅使用一种日期格式)。

It's hard to say exactly what is going on without knowing precisely what string you are passing into [dateFormat dateFromString:] in both cases. 在这两种情况下,都很难确切地说出到底发生了什么,而又不确切知道[dateFormat dateFromString:]中传递什么字符串。 You should post more specific details to help narrow it down. 您应该发布更多具体细节,以帮助缩小范围。

Without knowing more, the only obvious difference I see between the two links is that they appear to use different text encodings. 在不了解更多信息的情况下,我看到的两个链接之间唯一明显的区别是它们似乎使用了不同的文本编码。 If you are parsing the same way without accounting for encoding differences it is possible that the contents of [item objectForKey:] is not actually the same between these two RSS feeds. 如果在不考虑编码差异的情况下以相同的方式进行解析,则这两个RSS feed之间的[item objectForKey:]内容可能实际上并不相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM