Posted on by & filed under Content - Highlights and Reviews, Mobile Development, Programming & Development.

XML parsing is an essential element for client-server applications. Most of the data transferred between client and server is in XML format, and generally, the XML data received from the server is of XML-RPC, SOAP, REST or RSS standard.

In this post we will use NSXMLParserDelegate for parsing XML, which has the following methods:

In our example, we will fetch the RSS feeds of a website, parse them, and show the results in NSLog.

Start XCode and create a new iOS application (or any other application you like – this post focuses on an iOS app). To learn more about using XCode, read this chapter in Foundation iPhone App Development: Build An iPhone App in 5 Days with iOS 6 SDK by Nick Kuh.

In the header file of view controller of the iOS app, implement the NSParserDelegate. You also need to declare two NSString properties: currentElement and currentTitle. Your header file should look similar to this:

Don’t forget to synthesize these properties in the implementation (.m) file.

Switch to the implementation (.m) file now and in the viewDidLoad method, we will fetch the raw XML data of the RSS we want to fetch. The code in your viewDidLoad method should look like this:

When the the XMLParser starts fetching data, the Invocation of delegate methods starts to occur. To learn more about NSURL, read this section in Programming in Objective-C, Fifth Edition by Stephen G. Kochan. We will override the delegate methods to parse the RSS feeds. First, write the following code in the didStartElement methods.

Here, didStartElement is invoked when NSXMLParser starts going through a new XML element in XML Data. As per the standard, RSS has an element named “title” (see In this post we will mine the data in the title element from the RSS Feed’s XML data and just show the parsed titles in NSLog.

In the code above, we set the currentElement property to the elementName argument. Then we define an IF statement that states that if the currentElement property is equal to title (if the XML Parser is going through the title XML element), then we just initialize the currentTitle property as a new NSMutableString.

Next, define the foundCharacters method as follows:

Note that the foundCharacters method is invoked when the parser finds some data in the XML tags. There will be two IF conditions in this method. Since this method is called often at different states, we will make sure that if the currentElement property is nil, then the method should return immediately and does not run further. Otherwise, we will define another IF condition that states that if the currentElement is equal to the string with a value of title, then the currentTitle will be inserted/added with the value of the string attribute.

At last, define the didEndElement method as follows:

Here, the didEndElement is invoked when the parser is going through the end of the XML tag (that is the closing element tag, for example </title>). Since we are just mining the data in the title element, define an IF statement that states that if the currentElement property is equal to title (if the XML Parser is going through the ending </title> XML element), then just NSLog the literal in the currentTitle property. Since it is the last method we are considering in the invocation life cycle, set the currentTitle and currentElement properties to nil.

We are finished! Run the app and click the Go To Table button and it should work out-of-the-box.

Safari Books Online has the content you need

Check out these Objective-C books available from Safari Books Online:

Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apple’s iOS and OS X platforms. The book makes no assumptions about prior experience with object-oriented programming languages or with the C language (which Objective-C is based upon). Because of this, both beginners and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C. Readers can also learn the concepts of object-oriented programming without having to first learn all of the intricacies of the underlying C programming language.
As iOS apps become increasingly complex and business-critical, iOS developers must ensure consistently superior code quality. This means adopting best practices for creating and testing iOS apps. Test-Driven Development (TDD) is one of the most powerful of these best practices. Test-Driven iOS Development is the first book 100% focused on helping you successfully implement TDD and unit testing in an iOS environment.
Foundation iPhone App Development: Build An iPhone App in 5 Days with iOS 6 SDK quickly enables existing programmers to become familiar and comfortable coding Objective-C using Xcode 4.5, Storyboarding and the iOS 6 SDK to create apps for the iPhone. Nick Kuh, an experienced, Apple award-winning developer, will teach readers how to build an iOS 6 iPhone app from start to finish in 5 days.
HTML5 Canvas Cookbook begins by covering the basics of the HTML5 Canvas API and then provides techniques for handling features not directly supported by the API such as animations and canvas interactivity. It winds up by providing detailed templates for a few of the most common HTML5 canvas application.
If you’re a web developer looking to use this new version of HTML, you might be wondering how much has really changed. Head First HTML5 Programming introduces the key features — including improved text elements, audio and video tags, geolocation, and the Canvas drawing surface — and uses concrete examples and exercises to apply and reinforce these concepts.
In these video LiveLessons, Jiva Devoe helps the viewer understand the key concepts and fundamentals of the Objective-C language. Objective-C Programming Fundamentals LiveLessons (Video Training) covers basic objective-c syntax, objects, memory management, protocols, the foundation framework and errors. The newest features in Objective-C such as ARC, blocks, and properties are included. After watching this video, viewers should be able to build and run basic Objective-c programs.

About the author

Umair Khan Jadoon is an avid developer. He enjoys building awesome apps for mobile and web and occasionally writing about technology. Geek at heart, he’s working on establishing his own mobile app startup.

Tags: iOS, JSON, NSXML Parser, NSXMLParser, Objective-C, RSS, Xcode, XML,

2 Responses to “Parsing JSON in Objective-C using NSXML Parser”

  1. Behrang

    Any plans to support marking/highlighting text and adding notes in Safari 2 Go for iPad?


  2. Bill Levien


    There is currently no ETA for launching highlights, notes & tags in the native apps, but I will make an announcement on the blogs if we start development on that toolset.