Think2Loud

06 Sep, 2008

Reading XML with jQuery

Posted by: Jared In: JavaScript

Welcome to my first of many articles for Think2Loud! Today I thought I’d start with something simple that will lead into several other topics. So here we go, head first into reading XML with jQuery.

The first step is making sure you have jQuery included on your page.

<script src="jquery.js" type="text/javascript"></script>

Second, you should check the XML file you are going to read and make sure it’s free of errors. If you are unfamiliar with XML synatax, check out the W3School’s rules on XML syntax. The file I’m using for this example is just a listing of some Web sites I enjoy.

Finally, you just need to tell jQuery to read the file and print out the contents. Start by adding the document.ready function to the page.

$(document).ready(function(){
 
});


Within the document.ready we start a jQuery ajax request to read the XML file. The ajax request takes four parameters: file type, url, dataType, and success. You’ll see below how these are set to read a file. The important thing to notice is the success parameter. We set it to a function that takes the data the request gets from the XML file.

$.ajax({
        type: "GET",
	url: "sites.xml",
	dataType: "xml",
	success: function(xml) {
 
	}
});

This is where the fun part comes in. Now that we are reading the XML, we need to find the data written within it and do something with it. We start by reading the returned data and using the find method to get all the nodes that match the text we supply (in this case, “site”), and then use the each function to loop through what the find function give to us.

$(xml).find('site').each(function(){
 
});

All that’s left is to get the data from that node and print it out. Do this by using the attr function, and the find function to get the text and any attributes on the nodes.

var id = $(this).attr('id');
var title = $(this).find('title').text();
var url = $(this).find('url').text();
$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');

Your final code should look something like this:

$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "sites.xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('site').each(function(){
				var id = $(this).attr('id');
				var title = $(this).find('title').text();
				var url = $(this).find('url').text();
				$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
				$(this).find('desc').each(function(){
					var brief = $(this).find('brief').text();
					var long = $(this).find('long').text();
					$('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
					$('<div class="long"></div>').html(long).appendTo('#link_'+id);
				});
			});
		}
	});
});

That’s all it takes to read XML with jQuery. Feel free to check out the demo and download the source code.

Tags: ,

8 Responses to "Reading XML with jQuery"

1 | I wrote my first tutorial! : jaredharbour.com

September 6th, 2008 at 7:48 pm

Avatar

[...] Go check it out, Reading XML with jQuery. [...]

2 | links for 2008-09-08 « Brent Sordyl’s Blog

September 8th, 2008 at 8:31 am

Avatar

[...] Reading XML with jQuery head first into reading XML with jQuery. (tags: jquery) [...]

3 | EllisGL

September 10th, 2008 at 7:09 am

Avatar

How well does it go against large XML Files, Say, 5 meg?

4 | Weekly Link Post 59 « Rhonda Tipton’s WebLog

September 12th, 2008 at 3:21 pm

Avatar

[...] Good article on Reading XML with jQuery. [...]

6 | Bud Gibson

October 13th, 2008 at 5:34 am

Avatar

This is a great, clear example. I’d be interested to see you address namespaces. There’s no real solution for those because of strong browser issues, but there are workable “solutions”.

7 | A developer

December 3rd, 2008 at 7:32 am

Avatar

Thank you.
But it doesnt seem to work on firefox

8 | Josh

December 3rd, 2008 at 7:43 am

Avatar

@A developer

What version of firefox are you using? The demo works for me in firefox 2 and 3 on OSX.

Comment Form