Thursday 6 December 2012

Use jQuery to Retrieve Data From an XML File

Use jQuery to Retrieve Data From an XML File

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.

Load XML With jQuery



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" ?>
<books>
    <book title="CSS Mastery" imageurl="images/css.jpg">
    <description>
    info goes here.
    </description>
    </book>
    <book title="Professional ASP.NET" imageurl="images/asp.jpg">
    <description>
    info goes here.
    </description>
    </book>
    <book title="Learning jQuery" imageurl="images/lj.jpg">
    <description>
    info goes here.
    </description>
    </book>
</books>
Next, let’s take a look at the actual jQuery.
  1.     $(document).ready(function()  
  2.       {  
  3.         $.get('myData.xml'function(d){  
  4.         $('body').append('<h1> Recommended Web Development Books </h1>');  
  5.         $('body').append('<dl>');  
  6.         $(d).find('book').each(function(){  
  7.             var $book = $(this);  
  8.             var title = $book.attr("title");  
  9.             var description = $book.find('description').text();  
  10.             var imageurl = $book.attr('imageurl');  
  11.             var html = '<dt> <img class="bookImage" alt="" src="' + imageurl + '" /> </dt>';  
  12.             html += '<dd> <span class="loadingPic" alt="Loading" />';  
  13.             html += '<p class="title">' + title + '</p>';  
  14.             html += '<p> ' + description + '</p>' ;  
  15.             html += '</dd>';  
  16.             $('dl').append($(html));  
  17.             $('.loadingPic').fadeOut(1400);  
  18.         });  
  19.     });  
  20. });  
  21. </dl>  

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.

No comments:

Post a Comment

Do you think it could be useful for you? Share your thoughts with us!