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

Importing data from XML to SQLite

From: Android SDK: Local Data Storage

Video: Importing data from XML to SQLite

I've previously described how to read data from an XML file, showing you two different parsers that you can use, and I've also described how to create new rows in an SQLite database table. I'm going to combine these two features. I'm going to use an XML file to seed my application with data and then move the data over to the SQLite database as the application launches for the first time. I'll be doing all of the work in the main activity class in this version of my project, ImportToDb.

Importing data from XML to SQLite

I've previously described how to read data from an XML file, showing you two different parsers that you can use, and I've also described how to create new rows in an SQLite database table. I'm going to combine these two features. I'm going to use an XML file to seed my application with data and then move the data over to the SQLite database as the application launches for the first time. I'll be doing all of the work in the main activity class in this version of my project, ImportToDb.

In the main activity class down at the bottom of the class I have a method called Create data, and right now I'm creating some fake data. I'd like to replace this code with code that imports from XML and exports to the database. So I'm just going to select and delete all of this code. If you need this code back again, you can find it in the previous version of the project in the Solutions folder. I'll go get code to import data from an XML file, I'll move back up to the On Create method, and I'll locate this code that I commented out in a previous exercise.

It uses the XML Pull Parser to read the data from an XML file and returns it as a list of tour objects. I'll cut that code to the clipboard, then I'll go back down to my Create Data method, and I'll paste the code back in and then I'll uncomment it. You might need to do a quick fix to add an import statement for the ToursPullParser class. So now I have the tours from the XML file in memory, and I can loop through and easily call the methods in my data source object to pass the data to my SQLite database.

I'll use a foreach loop to loop through the list of tours. I'll type foreach, all one word, I'll press Ctrl+Spacebar, and choose the foreach code template. Eclipse automatically picks up that it's going to loop through the tours collection and create a tour object for each item in the collection. So I'll Tab a few times, and now my cursor is within the for loop. Within the for loop, I now have a tour object, and all I need to do is call the Create method of my data source class.

Remember, when I created the data source class which is in the db package, it receives a tour object and it adds it to the database table as a new row. So I'll go back to the activity and I'll simply call it datasource.create and I'll pass in the tour object. And that's it. I'm pulling the data from the XML file and passing it to the database. Everything else in this application will remain exactly as it was before. Up above, in the Uncreate method I'm doing a findAll to get the data from the tour's table, then I'm checking the size, and if it's 0, I'm calling this createData that I just modified to pass the data in, and then I'm retrieving the data again.

I'll save my changes. Now before I test this, I'm going to go to the emulator and clear all of the existing data to make sure I'm getting a fresh start. I'll go to the emulator and then I'll go to the Applications list. I'll click and hold on my application icon, I'll drag it up to App info, and then after a moment the emulator will let me clear the data. Then I'll go back to the Home screen, I'll return to Eclipse, and I'll run the application in the emulator again.

Now as the application opens in the emulator, it will query the tour's table. It will determine that there isn't any data in the table. It'll then call the createData method. It will retrieve the data from the XML file, pass it into the database, requery the database, and then display the data. So now you have a good example of how you can seed an application with data that you package up as an application resource. To review a couple of things from previous lessons, my XML file is a resource in the resources folder.

I placed it in a folder called raw, and it's a file called tours.xml. By putting it in the Resources folder, you make it a resource that you can address with the Resource ID, just like a drawable image, just like a menu item, or a user interface control. As another reminder, I'm using this class, ToursPullParser. But if you prefer, you can use the JDOMParser. Either one will do the job, and which one you use is a matter of taste, depending on how you like to do your coding and also want a performance because the two parsers return different results depending on the nature of the XML and the context of your application.

Finally, in the MainActivity I've added simple logic to determine whether there's data in the Application already, and if not, I've added code to move the data from XML to the database.

Show transcript

This video is part of

Image for Android SDK: Local Data Storage
Android SDK: Local Data Storage

29 video lessons · 6397 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 6m 34s
    1. Welcome
      50s
    2. What you should know before starting this course
      2m 11s
    3. Using the exercise files
      3m 33s
  2. 21m 6s
    1. Exploring local data storage options
      5m 58s
    2. Configuring Eclipse and the Android Developer Tools
      5m 29s
    3. Creating an Android virtual device
      3m 23s
    4. Creating a new Android project
      6m 16s
  3. 31m 30s
    1. Using preferences in Android apps
      5m 28s
    2. Defining preferences with Java
      9m 31s
    3. Defining shared preferences with an activity
      10m 50s
    4. Listening for changes to shared preferences
      5m 41s
  4. 51m 13s
    1. Creating and reading files in internal storage
      10m 18s
    2. Creating and reading JSON data files
      9m 48s
    3. Working with files in external storage
      11m 22s
    4. Parsing a read-only XML file with XmlPullParser
      11m 46s
    5. Parsing a read-only XML file with JDOM
      7m 59s
  5. 1h 8m
    1. Creating a new SQLite database
      2m 52s
    2. Defining a database with SQLiteOpenHelper
      12m 12s
    3. Managing the database with a DataSource class
      9m 38s
    4. Inserting data into a database table
      10m 29s
    5. Retrieving and displaying data
      11m 44s
    6. Importing data from XML to SQLite
      5m 15s
    7. Filtering and sorting data
      9m 27s
    8. Accessing a database from the command line
      6m 46s
  6. 42m 0s
    1. Improving the data display
      9m 29s
    2. Passing user-selected data to a detail activity
      11m 36s
    3. Working with multiple database tables
      10m 28s
    4. Deleting data from database tables
      10m 27s
  7. 49s
    1. Where to go from here
      49s

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 Android SDK: Local Data Storage.

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.

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.