Under the Bridge

Tip: Sanitizing RSS Feeds

So let’s say you’ve got what’s supposed to be a simple RSS feed you’re parsing with NSXMLParser — but the clowns that put it together insist on providing it with strange Windows characters embedded in the text, so the parsing consistently chokes with illegal character errors. And, of course, the suggestion of getting Windows people to display even minimal competence on their end, well that’s just crazy talk. So, what to do? What to do?

Well, here’s a quick and easy trick to sanitize the offensiveness right out:

NSString *dataString = [[[NSString alloc] initWithData:rawFeedData encoding:NSASCIIStringEncoding] autorelease];
NSData *sanitizedData = [dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

Hey, worked for us!

h/t: StackOverflow!