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

Parsing XML is a processor intensive operation and mobile devices tend to have limited processing power. Even though Apple continues to improve the specs of each iteration of their devices, in order to preserve the responsiveness that these devices are known for, we'll be doing our parsing in a separate execution thread. This ensures that we don't block the main thread, which could cause the user interface to freeze during parsing. Let's start by making a working copy of BWRSS-XML-03. I'm going to use the -done version here, and I'm just option dragging to make a copy and rename that to -04.

Parsing the feed with NSXMLParser

Parsing XML is a processor intensive operation and mobile devices tend to have limited processing power. Even though Apple continues to improve the specs of each iteration of their devices, in order to preserve the responsiveness that these devices are known for, we'll be doing our parsing in a separate execution thread. This ensures that we don't block the main thread, which could cause the user interface to freeze during parsing. Let's start by making a working copy of BWRSS-XML-03. I'm going to use the -done version here, and I'm just option dragging to make a copy and rename that to -04.

I'll open the project in Xcode by double-clicking on this Xcode project file. Now, remember we have this NSLog line, which says we have data, and that happens when we finish loading from the NSURLConnection. So, if I run this in the iPhone Simulator, I select the iPhone Simulator and click Run. When I click on one of these feeds, you'll notice we'll get a message down here in the log that says have data. Click on another feed, have data.

So, I'll go ahead and stop this from running in the Simulator by selecting Xcode and pressing Command+Period. I'm just going to minimize our log area down there. Now, we're going to bring in a whole bunch of code in our text editor, because it's just way too much typing for us to do here in this lesson. So, you'll see the file here TableViewController-04-NSXMLParser.txt. I'm going to open that in my text editor, I'm using TextWrangler here. I'm going to go ahead and select all of the file, you'll notice that there's just a whole lot of code in here, and we'll go through this, and we'll take a look at it.

We're going to do that in Xcode. So, I'm going to Copy that with Command+C and select Xcode. I'm going to come all the way down here to the end. I'm going to put this in just before the Error handling here, and I'll Paste that with Command+V and press Command+S to Save. So, if we look at what we've added to the file here, we have the NSLXMLParser delegate methods. So, the way this works is when we set up the Parser, we set our own class as the parser delegate and then the parser calls the delegate methods inside of our class at each step in the parsing process so that we can process the data.

We have a few little Utility methods, and we'll look at these. These mostly have to do with formatting dates and these error handlers were in there before. So, if we look at didStartElement, this is a parser delegate that's called at the beginning of each element in an XML stream. Basically, what we do at the beginning of an element, depending on the type of an element, if it's a container, we just set things up to start accumulating data, and if it's not on a container, then we decide what to do individually based on that. For instance, if it's the ChannelElement, we start setting up the feed and item objects.

If it's an ItemElement, then we know that we've completed the item before, and we can use the contents of that item to start populating the database. If it's one of our containerElements then we just start accumulating data. DidEndElement tells us that we've gotten to the end of the element and especially in the case of the containerElements, it tells us that we can start adding items to the list, because we've reached the end of a container and all of the content of that container that's been accumulated, we can start adding that to the Items List.

In particular, if we have one of these dateElements, so these are really just different names for the PubDateElement, but then we need to clean up the date format, and if we come down here into our Utilities and look at dateStringToSQLDate, of all these different possible formats that I've discovered in RSS Feeds, and I know that there's more. There are probably still some that don't work with this app, because they have these different date formats. They are supposed to use a standardized format for their dates, but everybody uses a different format, and I don't know why.

It's just annoying mostly, but this is how we deal with it. So, we parse in from these different dates, we use the built-in dateFormatter in iOS. It's the same as in OS X and then we simply format it into the standard SQL format for storing in the database. That makes it possible. Compare dates, it makes it possible to sort dates, because this is a really sensible format for computer processing. Now, especially for our containers whenever we find characters, we accumulate that and at different points in the process we say, okay, we're done with this chunk of data, and we'll go ahead and process it.

And if there is an error, then we call handleError on the main thread and of course, handleError is the same as we've used elsewhere in this application for, especially in the NSURLConnection methods. So, we come back up here to our Support methods, parseRSSData that sets up the NSXMLParser in a separate thread. You notice that we have to put all of this code in this autoreleasepool that's because it's running in the secondary thread, and this is the new syntax for doing this with ARC, Automatic Reference Counting, starting in iOS 5.

In our addItemsToList method, this is where we add items to the database. Now because we haven't set up our Table methods yet, we're not going to be displaying this on the screen. So, we're going to put in here an NSlog so that we can see when we actually have the parser data, because we're not going to be actually displaying it on the screen yet, because we haven't set up those methods yet for handling the table. We'll do that in the next movie. For right now we just want to say item and use that for the title.

We'll put the publication date in parentheses, and we'll put the URL in square brackets. I'm a big fan of copy and paste. We're going to use our constants for keying off the Item here, kItemTitleKey and kItemPubDate and kItemUrlKey. It's looking like it's got a little warning here. Ah, yes, typo.

Here we go and press Command+S to Save. Now, we need to come back here to connectionDidFinishLoading, and you remember we have this NSLogin there that says have data. I'm going to go ahead and take that out, and I'm going to uncomment this detachNewThreadSelector so that we can go ahead and run the parser in our secondary thread. So you see there's our call to parseRSSData as the selector getting passed to detachNewThreadSelector, Target:self, Object: self.rssData.

I'll go ahead and press Command+S, and we are going to Build, I'm going to press Command+B on my keyboard here and Build Succeeded. No problems. We can go ahead and run this in the iPhone Simulator. Now, when I click on one of these, you see down here we got our parsed data, and you can see here is item: On Choosing a Programming Language, that's the title, there's the publication date in the parentheses there.

Down here in the square brackets, that's the URL associated with that. So, I can go ahead and click on one of these other feeds, and you see we got a lot of data there for that feed. I can click on the final feed, and we get even more data. So, now we're successfully parsing the feed, and we're updating the Item Table in the database with the Items from the RSS Feed. At this point, all that's left to do is to update the TableView with the Items from the database just as we did with the feeds in the FeedsTableViewController.

So, I'll go ahead and quit the Simulator and minimize this and get ready for the next lesson.

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 · 6432 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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.