Parsing An XML File

This is the “XMLParsing” example. In this app we will read data from xml file and display the data on the screen.

Step 1: Open the Xcode and create a new Xcode project using Navigation base application template. Give the application name “XML”. As shown in the figure below:

Step 2: Expand classes and notice Interface Builder created the RootViewController.h and RootViewController.m class for you. Expand Resources and notice the template generated a separate nib, RootViewController.xib.

Step 3: We need to add another file. Right-click on the Classes folder and choose Add -> New File. Under Cocoa Touch Class category choose Objective-C class. Name it Book.h and Book.m file.
This will be a very simple class that will take our dummy data file, read it in as an NSArray and provide some utility methods to access the data from the file.

 

 

Step 4: We need to add Book.m file.

 

 

Step 5: We need to add another file. Right-click on the Resource folder and choose Add -> New File. Under Resource category choose View-XIB class. Name it BookDetailView.xib file. Double click the file and select Grouped table view.

Step 6: We need to add another file. Right-click on the Classes folder and choose Add -> New File. Under Cocoa Touch Class category choose Objective-C class. Name it XMLParser.h and XMLParser.m file. This will be a very simple class that will take our dummy data file, read it in as an NSArray and provide some utility methods to access the data from the file.

 

 

Step 7: We need to open XMLParser.m file.

 

 

Step 8: We need to add another file. Right-click on the Classes folder and choose Add -> New File. Under Cocoa Touch Class category choose UIViewController class. Name it BookDetailViewController.h and BookDetailViewController.m file.

 

 

Step 9: We need to open BookDetailViewController.m file.

 

 

Step 10: We need to open XmlAppDelegate.h file.

 

 

Step 11: We need to open XmlAppDelegate.m file.

 

 

Step 12: Now build and run the code and view the Output in the Simulator.

You can download source code from here XML

Leave a Comment:

27 comments
abuyaqeen says April 15, 2011

thank you very much ..

Reply
Patrick says April 17, 2011

Thanks!

Reply
J.L.G says May 7, 2011

thanks for such tutorial.

i have a couple questions. is there any way to contact you directly?

Reply
    ramesh says August 14, 2012

    yes

    Reply
Pulkit says May 10, 2011

Thanks you for putting this together.

I followed the tutorial closely and the following lines were the only ones missing (in BookDetailViewController.m) that weren’t mentioned explicitly and prevented me from building the code:
#import “Book.h”
@synthesize aBook;

… After I downloaded your source, I was able to peek inside and resolve this. But the build didn’t show the expected visual output. It seems the older auto project generated root view controller was still hooked up to the window. I didn’t have the motivation to dig deeper into the source as I’m a beginner but it would be fantastic if you could actively talk about how to clean up the auto-generated views and wire up the one in your tutorial to an actual window 🙂

Reply
Brad Robinson says May 14, 2011

Hi

Great tutorial but i just had one question, i would like to show an image either in the current detail view or in a new detail view possibly accessed by a button at the bottom of the current view any help would be much appreciated.

If you think that the button at the bottom is the best idea i would like the url for the image loaded into the button from the xml data on the internet.

As i said i would really appreciate the help as i am still a beginner and would like to be able to complete this app.

Thanks
Brad

Reply
Ross says May 19, 2011

I changed the link in the app delegate .m file to my own XML file on the internet and now the app builds but crashes with the error “terminate called after throwing an instance of ‘NSException’ ” I’ve also tried changing the link to a different XML file on the same site with identical results. (http://chakrainteractive.com/mob/0-vin/Books.xml)

Any idea what is going on?

Reply
kalpesh says May 31, 2011

@interface XMLParser : NSObject

{
}
@end

need little bit Change…Not ..Neccesary.
Here You will Found How To use Delegate…
Or A Class As Delegate…Make Differance…

thanks …

Reply
DonOnGuitar says June 1, 2011

The link to your XML file gives 404 error.
http://chakrainteractive.com/mob/0-vin/Data.xml

Reply
    Sushant says June 2, 2011

    i have changed the link , you can checked now and also updated the code.

    Reply
      Ross says June 6, 2011

      Sushant: Just downloaded your updated code, and now the table is completely empty. Just a heads up.

      Reply
        Sushant says June 9, 2011

        I have downloaded SourceCode from post and compile it. Working perfectly. You try it once again.

        Reply
Brad Robinson says June 2, 2011

hi guys

thanks for the info about the image and simpleURL, i have another problem now i want to apply a search facility to the table but the problem i have is that all the code on the internet is showing that you need to make a copy of the array before you start searching but i am not sure how this will be accomplished importing the array from another class.

PLEASE HELP!!

Thanks
Brad

Reply
Rahul says July 28, 2011

Hi, I am trying to get the data from the following website and loading it in the UITableview as per your tutorial, but my tableview is loading blank. Any suggestions.
http://iaspub.epa.gov/enviro/efservice/PWS/State/MD/status/Active
Thanks!

Reply
david boon says August 27, 2011

sushant bhai its showing me a error like “pattern colors on iOS versions prior to 3.0, as well the build is failed… what to do?

thanks in advance

Reply
How to save a file from an URL to local in Objective-c says October 16, 2011

[…] to a tutorial, the code in XmlAppDelegate.m file as below is going to parse the XML file directly from a given […]

Reply
Micha Radi says November 6, 2011

How do you do all of this in Tab Bar Application which has two view controllers; one with this (parsing an XML file) and another one with only a UIViewController. I know how to do the 2nd controller, but I cannot figure out how to do the first one.

Because in the UINavigation App, you parsed the XML file from the delegate itself. Is it the same with UITabBar App?

Thanks a million!

Micha

Reply
antuks says December 13, 2011

I was able to run your code – thanks!

My only problem is when I added a new View (this is my login) before the RootViewController – when I click the button it shows the Main table view but when I click the data on each row it didn\’t show the details view like nothing happened. Any idea what I did wrong? Details View works flawlessly without the login View

Reply
Javi says December 14, 2011

i downloaded the code and works perfect, but if i putmy file XML in xcode how i can parse it, this is for a url for internet but i have a local file, can you help me?

Reply
James says February 20, 2012

Have you seen Pegasus? It’s a framework that lets you create the ENTIRE UI out of XML:

https://github.com/jonathanellis/pegasus

Reply
srikanth says September 23, 2012

i don’t know how to use the JSON services.
please can u help me with code.

Reply
sapphire says February 5, 2013

i want to show image in table.how to do??

Reply
sapphire says February 5, 2013

http://d1a82sglq4gqtq.cloudfront.net/IntProviderList.xml
this is my XML file

i want to show image in table view.

now i can show name only.

Reply
CopyQuery | Question & Answer Tool for your Technical Queries says October 7, 2013

[…] download this project and opened him into Xcode 5, but in the cells do not show labels. In other my projects the […]

Reply
Add Your Reply