Reputation: 9
I am trying to parse XML file of RSS News. First, I tried the RSS from http://ria.ru/export/rss2/index.xml
and everything worked perfectly. Then I tried another resource, namely http://interfax.ru/rss.asp
and I encountered a problem with the date:
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. Foor example:
Wed, 25 Jan 2012 16:41:00 +0400
However, the second RSS with same date format gives me NULL
. Both stringTime
and dateFormatFromString
produces NULL
values as well. I changed the address RSS with the same format date and still have this problem.
Upvotes: 0
Views: 578
Reputation: 5128
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. Even though there are specifications, many RSS feeds don't follow them.
One approach is to have an array of date formats, and enumerate through it until a non-nil string is returned:
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).
Upvotes: 1
Reputation: 8292
It's hard to say exactly what is going on without knowing precisely what string you are passing into [dateFormat dateFromString:]
in both cases. 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.
Upvotes: 0