Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Parsing the feed with NSXMLParser

From: iOS SDK and SQLite: Building Data-Driven Apps

Video: Parsing the feed with NSXMLParser

Now that we have an RSS feed, we need to run NSXMLParser to get the title and description of the feed. These will be stored in the database and used for displaying the main feed table view. In this movie, we'll concentrate on parsing the feed. We'll start by making a working copy of BWRSS-addView-03, and I'm going to use the -done version. You can use the version that you worked on in the last movie if you're following along. I'm going to rename this as addView-04 and open it in Xcode by double-clicking on the Xcode project file.

Parsing the feed with NSXMLParser

Now that we have an RSS feed, we need to run NSXMLParser to get the title and description of the feed. These will be stored in the database and used for displaying the main feed table view. In this movie, we'll concentrate on parsing the feed. We'll start by making a working copy of BWRSS-addView-03, and I'm going to use the -done version. You can use the version that you worked on in the last movie if you're following along. I'm going to rename this as addView-04 and open it in Xcode by double-clicking on the Xcode project file.

I'm going to come back out to the Finder now, and I'm going to grab this methods.txt file, and I'm just going to select all of this. This is just all of the parser code. And I'll select all with Command+A and Command+C to Copy, and come back here into Xcode and drop it in here at the bottom before the end tag. Command+S to save, and we're going to back up here to connectionDidFinishLoading, and where we have this self parseRSSHeader, I'm going to just uncomment that. And then we're going to come up to haveFeed, which is in our RSS Feed Management. And where we call the delegate haveAddViewRecord-- we haven't implemented that delegate code yet-- I'm going to comment that out.

I'm also going to comment out this dismissViewController so that we can see this result. And I'm going to put in a status message, self statusMessage:@* Have feed, and it's name, which will be from feedRecord kTitleKey, and Command+S to save. Now, let's go ahead and run it in the Simulator. And we'll add that CNN feed again. And we have the feed, and you can see it says, "CNN.com Top Stories," and that means that we are successfully parsing the feed because that's the title of the feed.

So, I'll press Cancel just to dismiss our view controller and come back over here to Xcode and press Command+. to stop the simulator. Let's take a look at the parsing process. Again, this is very simple and straightforward. We call the parser, and the parser starts its process and uses these callback methods, these delegate methods, in order to allow us to control the different parts of the parsing process, and to extract our data where we need to. DidStartDocument allows us to set up our feed record to MutableDictionary. didStartElement gets called at the beginning of each element and allows us to finish up the element before, and to store some data. And you'll notice that there is something here that's not working in iOS 6, and I'll get back to that in a moment.

Parser didEndElement gets called at the end of each element. And again, it allows us to process the data from that element. parser foundCharacters for elements that are containers, this allows us to accumulate date within those containers. And parseErrorOccurred gets called whenever there is an error, and when Abort is called, although that's not working in iOS 6. So, let's come back up here to didEndElement, and you see this if (haveTitle && haveDescription), didFinishParsing = TRUE, and then a call to parser abortParsing.

Now, what's supposed to happen when you call parser abortParsing is you're supposed to get an error message sent to parseErrorOccurred saying that the parsing had been aborted. And this used to work fine and previous versions of this code used this because we're just looking for a few items in the top part of the XML file; we don't need to parse an entire 100k XML file just to find the first few entries in the file, and that's what this is supposed to before. So, when we get our title and our description, we want to just abort at that point and stop wasting everybody's time and energy. And this used to work in previous versions. Starting in beta 4 of iOS 6, this did not work anymore. And I was finding that existing versions of my RSS reader had just stopped working on iOS 6, so I hunted around, I found the problem, and I found the workaround, and I sent a bug report to Apple and they have acknowledged that. But it has not yet been fixed, and we're now on iOS 6.1 as in beta as I'm recording this, and this particular bug still hasn't been fixed.

So, I've just taken it out. We're not doing that anymore. And we're parsing the entire file, although you'll notice that that I set a flag that says didFinishParsing, and I have this in a lot of these methods, if (didFinishParsing), and I just return, so that I'm still not wasting anymore energy than I need to be. So, we're now successfully parsing the header of the RSS feed, and as you can see, this is a lot less complicated than the parser in the item view controller because here, really, we're only looking for a couple of things. The delegation pattern used by NSXML parser and also used by a lot of the Cocoa framework is flexible enough that we can really just pay attention to the parts that matter, giving us the simplicity where we need it or the complexity where we might need that.

Show transcript

This video is part of

Image for iOS SDK and SQLite: Building Data-Driven Apps
iOS SDK and SQLite: Building Data-Driven Apps

41 video lessons · 6886 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 8m 29s
    1. Welcome
      57s
    2. Exercise files
      2m 17s
    3. Course overview
      3m 4s
    4. Application overview
      2m 11s
  2. 14m 49s
    1. Prototyping in a testbed
      1m 27s
    2. Building the view controller
      3m 45s
    3. Coding the testbed
      7m 56s
    4. Using the testbed
      1m 41s
  3. 37m 49s
    1. Understanding SQLite in iOS
      1m 41s
    2. Creating an Objective-C interface for SQLite
      9m 57s
    3. Testing the BWDB interface in the sandbox
      7m 1s
    4. Designing a database schema
      8m 7s
    5. Supporting the application with a specific interface
      7m 7s
    6. Using C pointers with automatic reference counting (ARC)
      3m 56s
  4. 21m 18s
    1. Understanding the table view
      1m 33s
    2. Creating the view controller
      6m 39s
    3. Reading from the database
      13m 6s
  5. 33m 50s
    1. Understanding the parsing process
      1m 57s
    2. Creating the item view controller
      12m 25s
    3. Reading data from the internet
      5m 30s
    4. Parsing the feed with NSXMLParser
      8m 2s
    5. Updating the item view with the feed items
      5m 56s
  6. 40m 14s
    1. Understanding the modal view
      1m 47s
    2. Constructing the view controller
      15m 5s
    3. Finding a feed link in a web page
      8m 55s
    4. Parsing the feed with NSXMLParser
      5m 4s
    5. Delegating back to the parent view
      6m 11s
    6. Deleting feeds
      3m 12s
  7. 21m 5s
    1. Creating the web view class
      12m 33s
    2. Coding the web view
      5m 25s
    3. Viewing pages in Safari
      3m 7s
  8. 14m 3s
    1. Understanding the iOS preferences system
      1m 23s
    2. Creating the preferences plist in Xcode
      7m 20s
    3. Reading preferences in your application
      5m 20s
  9. 6m 15s
    1. Adding pull-to-refresh functionality
      2m 34s
    2. Implementing the pull-to-refresh gesture for iOS 6
      3m 41s
  10. 27m 1s
    1. Understanding split view
      1m 4s
    2. Coding the table views
      11m 24s
    3. Implementing the iPad detail view
      6m 35s
    4. Implementing the iPad modal view
      7m 58s
  11. 35s
    1. Goodbye
      35s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed iOS SDK and SQLite: Building Data-Driven Apps.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.