Quick Tip: Use jQuery to Retrieve Data From an XML File

Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

In this quick tip, I'll show you how to load data from an XML file onto a blank page. We'll work with the $.get function and will also implement a loading gif while the information is being retrieved. We'll be displaying a simple list of recommended web development books. Let's go ahead and get started.

Step One: Review The Script

First, let's take a look at our simple XML file. It merely contains a few books along with their title, image and description.

<?xml version="1.0" encoding="utf-8" ?>
    <book title="CSS Mastery" imageurl="images/css.jpg">
    info goes here.

    <book title="Professional ASP.NET" imageurl="images/asp.jpg">
    info goes here.

    <book title="Learning jQuery" imageurl="images/lj.jpg">
    info goes here.

Next, let's take a look at the actual jQuery.

        $.get('myData.xml', function(d){
        $('body').append('<h1> Recommended Web Development Books </h1>');
        $('body').append('<dl />');


            var $book = $(this); 
            var title = $book.attr("title");
            var description = $book.find('description').text();
            var imageurl = $book.attr('imageurl');

            var html = '<dt> <img class="bookImage" alt="" src="' + imageurl + '" /> </dt>';
            html += '<dd> <span class="loadingPic" alt="Loading" />';
            html += '<p class="title">' + title + '</p>';
            html += '<p> ' + description + '</p>' ;
            html += '</dd>';


Step Two: Decipher Time

Because this is a quick tip, I'll run through the script a bit quicker than I usually would. When the document is ready to be manipulated, we're going to fetch our XML file using the "$.get" function. In the parenthesis, we'll pass in the location of the file, and then we'll run a callback function. I'll use the variable "d" to represent the information that was pulled from the XML file. Next, we're going to create a heading tag and a definition list.

Continuing on, we're going to search through the XML file (d) and find the tag entitled "book". Referring back to my document, there are three books. Consequently, we'll need to run the "each" method in order to perform an operation for each book. Remember, "each" is just like the "for" statements. It's a way to run through each element in the wrapped set.

In the next block of code, I'm defining a few variables. In order to get the "title" and "description" from our XML file, we use ".attr(value)" - where "value" is equal to the attribute within the tag.

Lastly, we're creating a variable called "html" that will contain the html that will display the information from our XML file. Merely assigning that information to the variable won't display it on the page, however. To add it to the page, we grab the definition list and append the variable. - $('dl').append($(html));

One more thing worth mentioning is that, while the information is being retrieved from the XML file, we'll display a standard loading gif (via a background image). When the data has loaded, we'll grab the image and fade it out.

You're Done

I know I went through that somewhat quickly; So feel free to leave a comment and ask any questions that you might have. It should be noted that this script will require a bit more work before it becomes ready for a real world website. You have to compensate for people that have Javascript turned off. In this case, if they did have it off, they would be staring at a blank page. You must compensate for such things. But, I digress.