Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

–1 vote
969 views
in Technique[技术] by (71.8m points)

parsing xml using jquery and ajax

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...