04-10-2011

jQuery Parse RSS XML with jQuery

You can use the same jQuery traversal and selection methods for XML that you use for (X)HTML. this makes it very easy to parse an XML file and process it's contents if you already know jQuery.

Here's a quick example of an rss parsing script.

The XML file of course needs to be on the same domain or retrieved via a simple serverside proxy script, because of cross site scripting restrictions in JavaScript:

<script>
	var rssurl = 'nieuws.xml';
	$.get(rssurl, function(data) {
		var $xml = $(data);
		$xml.find("item").each(function() {
			var $this = $(this),
				item = {
					title: $this.find("title").text(),
					link: $this.find("link").text(),
					description: $this.find("description").text(),
					pubDate: $this.find("pubDate").text(),
					author: $this.find("author").text()
			}
			console.log(item)
		});
	});
</script>

Comments:

Your comment:

»
Clem 06/01/2012, 6:43 pm
This is great for rss text data. What do you do about an image that is passed? How is it processed and added to a <div> for example?

 

[x]