XML parsing is a frequently used thing in applications fetching data from the web. Fetching RSS feeds, XML-RPC, SOAP or REST data might not be usable unless you parse it. In Objective C (specifically in iOS development), NSXMLParser is used for parsing XML. The following tutorial explains how you can use NSXMLParser to parse RSS feeds.

Create a new Objective C application in XCode. I am creating an iOS application in this tutorial.

In your header file, (in this case, header file of a view controller of an iOS app) , make sure you implement the ‘NSXMLParserDelegate’. Also for this tutorial, declare two properties ‘currentElement’ and ‘currentTitle’;

@interface MyViewController : UIViewController <NSXMLParserDelegate>
@property(nonatomic, strong) NSString *currentElement;
@property(nonatomic, strong) NSString *currentTitle;
@end

In the implementation file, lets load the data of RSS Feed we want to parse, set the parser delegate to self and start parsing.

NSURL *rssURL =[[NSURL alloc] initWithString:@”http://blog.merrycode.com/feed”];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:rssURL];
[parser setDelegate:self];
[parser parse];

Now the implementation of 3 delegate methods that will parse the Titles of RSS feed items and NSLog them.

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
self.currentElement = elementName;
if([self.currentElement isEqualToString:@"title"])
{
self.currentTitle = [NSMutableString string];
}
}
 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([self.currentElement isEqualToString:@"title"])
{
NSLog(@”%@”,self.currentTitle);
}
self.currentTitle=nil;
self.currentElement=nil;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!self.currentElement) return;
if([self.currentElement isEqualToString:@"title"])
{
self.currentTitle=string;
}
}

How the above code works:

When you call the parse method for the NSXMLParser object. It starts a thread that goes through each element of the XML data. On each event of going through the XML, it calls the related delegate methods (As it is obvious). Here in the above example, I am just extracting the “Title” element of RSS feed for simplicity.

didStartElement method is called whenever the parser starts to read a new element.  self.currentTitle is being initilized whenever the started element is equal to “title”. Also self.currentElement is set to the current started element.

didEndElement method is called whenever the parser is at the closing tag of any element.  The value in the self.currentTitle (That is set in foundCharacters method) is NSLog’d. After that self.currentTitle and self.currentElement are set to nil.

foundCharacters method is called whenever the parser finds any values inside the XML. In this method, first I checked if self.currentElement is set to nil (the parser at the closing tag of any element), the method returns immediately. Otherwise, if the value of self.currentElement is equal to ‘title‘, self.currentTitle is set to foundCharacters NSString string parameter.