4

I have an rss feed for my podcast and essentially what I am trying to do is populate an html5 audio player with URLs within the RSS feed.

I figure the best way to go about this is to parse the links using ajax and then append them into the src of the audio player. I am aware of the same domain policy that would prevent me doing this with ajax, so I am using the cross domain ajax plugin (http://bit.ly/Jbi9iX) to get around this.

I am struggling to figure out exactly why the code below is not working for me, basically at this stage I simply want to append the url's within the RSS feed into the #results to show that its working, then I will add it to the src part of the audio player.

$(document).ready(function () {
    $.ajax({
        url: 'http://theresidency.libsyn.com/rss',
        type: 'GET',
        dataType: "xml",
        success: parseXml
    });
});

function parseXml(xml) {
var item = $(xml).find("item");

  $(item).each(function() {
    $("#results").append($("enclosure").attr("url").text() + "<br />");
  });

}

I am not getting any errors in chrome dev tools, and I have looked around at other examples but I can figure out what I'm doing wrong.

Here is an example of the xml/rss: http://pastebin.com/stuY495c And here is what I have so far uploaded: http://bit.ly/J9QHZc

Any help would be much appreciated so thanks in advance!

Michael
  • 807
  • 4
  • 14
  • 25

2 Answers2

6

Where exactly are you passing the data to the function, I think you need to do:

$(document).ready(function () {
    $.ajax({
        url: 'http://theresidency.libsyn.com/rss',
        type: 'GET',
        dataType: "xml",
        success: function(data) {
           parseXml(data);
        }
    });
});

function parseXml(xml) {
var item = $(xml).find("item");

  $(item).each(function() {
    $("#results").append($("enclosure").attr("url").text() + "<br />");
  });

}

or just:

$(document).ready(function () {
    $.ajax({
        url: 'http://theresidency.libsyn.com/rss',
        type: 'GET',
        dataType: "xml"
    }).done(function(xml) {
        $.each($("item", xml), function(i, e) {
            $("#results").append($("enclosure").attr("url").text() + "<br />");
        });
    });
});

EDIT:

Did some more fiddling with this, and came up with:

$(document).ready(function () {
    $.ajax({
        url: 'http://query.yahooapis.com/v1/public/yql?q=%20SELECT%20*%20FROM%20xml%20WHERE%20url%3D%22http%3A%2F%2Ftheresidency.libsyn.com%2Frss%22&format=json&callback=',
        dataType: "json"
    }).done(function(data) {
        $.each(data.query.results.rss.channel.item, function() {
            $("#results").append(this.enclosure.url + "<br />");
        });
    });
});​

I do believe that is what you are looking for, here's a DEMONSTRATION

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • thanks for the reply dude, I've updated my latest version but it still doesn't pull anything into the div :/ and i've tried both solutions – Michael May 13 '12 at 21:46
  • You're probably not getting anything back at all, try logging the returned data to the console, and look for allow origin errors aswell, as xml is'nt normally supported if this cross domain. – adeneo May 13 '12 at 22:52
  • @Michael - did some testing, and edited my answer with a solution that should work. – adeneo May 13 '12 at 23:16
  • I see this converts the XML to JSON which was something I was going to look into, and it worked perfectly my friend, thankyou very much! – Michael May 14 '12 at 00:25
  • Just done so there now, again many thanks dude, out of interest, if I were looking for the first link only, is there an easy way to go about it with this code? – Michael May 14 '12 at 00:31
  • 1
    yes, just access it like so : `var first = data.query.results.rss.channel.item[0].enclosure.url;` – adeneo May 14 '12 at 00:47
1

Turns out, parsing RSS is a bit more complicated than basic XML parsing. Don't worry though, google can do the job for you and return a json-object:

$(function() {
   parseRSS('http://theresidency.libsyn.com/rss');
});

function parseRSS(url) {
  $.ajax({
    url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
    dataType: 'json',
    success: function(data) {
      $.each(data.responseData.feed.entries, function() {
        $('#results').append(this.link + '<br />');
      })
    }
  });
}
Kvam
  • 2,148
  • 1
  • 20
  • 32
  • Ah I didn't know that, I've tried your solution - http://pastebin.com/bhGAXydM but can't seem to get it to work, am I constructing it wrong? – Michael May 13 '12 at 21:56
  • All you have to do is call parseRSS(url) directly: $(function () { parseRSS("http://theresidency.libsyn.com/rss"); }); – Kvam May 13 '12 at 21:59
  • I still can't manage it, tried http://pastebin.com/bhGAXydM but I'm now getting an uncaught syntax. – Michael May 13 '12 at 22:11
  • By the way, I messed up the previous comment. New to this site. Updated the original post instead. – Kvam May 13 '12 at 22:12
  • Not to worry amigo, that has done the trick, I completely misread your comment when I updated anyway. Thanks for the help, really appreciate it! – Michael May 13 '12 at 22:16
  • Oh actually I just noticed, its spitting out the url for the post, not the mp3 link which is held in the `enclosure` tag, I'm sure this is just a matter of changing `this.link` to `this.enclosure` but I'm getting undefined response, any ideas? – Michael May 13 '12 at 22:32
  • The returned object seems to be quite limited - only values inside of elements with both opening and closing tags are returned. The enclosure element doesn't have an inner value, just a bunch of attributes. I'd try jFeed, a jQuery plugin that deals with rss: https://github.com/jfhovinne/jFeed – Kvam May 13 '12 at 22:50
  • I'm actually using zrss on another part of the site which could do the same thing, was more wondering if it was possible to achieve with some of my own jQuery. @adeneo managed to achieve what I needed though, thanks for all the help! – Michael May 13 '12 at 23:46
  • For anyone finding this via Google: `This API is no longer available.` – MrLewk May 11 '18 at 10:25