[iPhone] Parsing RSS with NSXMLParser

One of the test apps I created while learning iPhone development was a FeedReader app using UITabbarController. Using TabbarController in combination with NavigationController, TableViewController and WebViewController was challenging enough for someone who just began with iPhone development. But more on that later ...

One of the problems I faced in this app was parsing the RSS feed for the selected site. I used NSXMLParser to parse the contents of RSS feed to display them in a table. But the parser was adding control characters (\n\t\t\t or \n\t\t) at the end of every element value it was parsing. These control characters weren't a problem for feed elements like title, summary, date, etc but for url element it was a big issue as the characters were getting appended to the article URL and the next step which was using UIWebView to show the article in browser page, was unable to load such a URL as its simply invalid for the website holding the main article.

Ex.

PHP:
  1. link = "http://bakery.cakephp.org/articles/view/exporting-data-to-csv-the-cakephp-way\n\t\t\t";
  2. date = "Fri, 23 Apr 2010 10:02:55 -0500\n\t\t\t";

Even escaping it with UTF8 as follows didn't solve the problem

C:
  1. NSString *storyURL = [NSString stringWithFormat:@"%@", self.selectedURL];
  2. NSString *webURL = [storyURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

and it kept converting the URL to

HTML:
  1. http://bakery.cakephp.org/articles/view/creating-pdf-files-with-cakephp-and-tcpdf%0A%09%09%09

After Google search failed to address my problem, I finally asked for help on Stackoverflow and I got exactly what I was looking for. I was suggested to use NSString's componentsSeparatedByCharactersInSet method to get rid of the control characters by simply exploding the string at control characters and then joining it back if needed. Following code explains what needs to be done

C:
  1. // Get the actual string
  2. NSString *storyURL = [NSString stringWithFormat:@"%@", self.selectedURL];
  3.  
  4. // Break it at control characters. This will give an array with multiple objects
  5. NSArray *brokenString = [storyURL componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]];
  6.  
  7. NSString *webURL = [NSString stringWithFormat:@"%@", [brokenString objectAtIndex:0]];

We are simply exploding the string at control character to get NSArray object. We are only interested in the string at index zero since that's the URL without any control characters in it. So, we store it in a separate string for further use.

That's it. The URL is now well formed and can be served to web view.

3 Responses to [iPhone] Parsing RSS with NSXMLParser

  1. Pierre September 28, 2010 at 9:10 am #

    Thank you so much! I have been struggling with this for hours.

    P.

Trackbacks/Pingbacks

  1. Tweets that mention [iPhone] Parsing RSS with NSXMLParser at SANIsoft – PHP for E Biz -- Topsy.com - August 2, 2010

    [...] This post was mentioned on Twitter by Amit Badkas, Rohan Faye, Abhishek Deshpande, Aditya Mooley, Abhishek Deshpande and others. Abhishek Deshpande said: #SANIosft Blog: [iPhone] Parsing RSS with NSXMLParser - http://bit.ly/c5r0VO #iphone via @adityamooley [...]

Leave a Reply