如何动态阅读RSS

时间:2010-02-24 01:22:50

标签: javascript jquery rss

我想使用jQuery阅读多个RSS提要。

我正在尝试编写一个灵活的函数,它只会获取RSS URL,并且它将仅输出它的标题和图像如何为多个RSS URL执行此操作?

3 个答案:

答案 0 :(得分:5)

最简单的方法是使用Google AJAX Feed API。他们有一个really simple example,很适合你想要的东西:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">

google.load("feeds", "1");

function initialize() {
  var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml");
  feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var div = document.createElement("div");
        div.appendChild(document.createTextNode(entry.title));
        container.appendChild(div);
      }
    }
  });
}
google.setOnLoadCallback(initialize);

</script>
<div id="feed"></div>

当然,您可以将jQuery与API混合使用,而不是使用本机DOM调用。

答案 1 :(得分:0)

您是否看过这个JQuery插件:http://plugins.jquery.com/project/jFeed

答案 2 :(得分:0)

派对有点晚了,但实际上我使用deviantART图库供稿做了类似的事情并显示了最新的缩略图。我将它包装成几个功能以便于使用:

function keratin_callback(elem, data)
{
    if (!data
        || !data.entries
        || data.entries.length < 1
        || !data.entries[0].mediaGroups
        || data.entries[0].mediaGroups.length < 1
        || !data.entries[0].mediaGroups[0].contents
        || data.entries[0].mediaGroups[0].contents.length < 1
        || !data.entries[0].mediaGroups[0].contents[0].thumbnails
        || data.entries[0].mediaGroups[0].contents[0].thumbnails.length < 1) {
      $("<span>Data returned from feed not in expected format.</span>").appendTo(elem);
      return;
    }

    var entry = data.entries[0];
    $("<img>").attr("src", entry.mediaGroups[0].contents[0].thumbnails[0].url)
               .appendTo(elem)
               .wrap("<a href="" + entry.link + "" title="Title: " + entry.title + "\nPublished: " + entry.publishedDate + "" rel="related" target="_blank"></a>");
}

function keratin(elem, url)
{
    //keratin written by adam james naylor - www.adamjamesnaylor.com
    if (!elem || elem.length < 1) return; //no element found
    $.ajax({
        //you could use document.location.protocol on the below line if your site uses HTTPS
        url: 'http:' + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url + '&cache=' + Date.UTC()),
        dataType: 'json',
        success: function(data) {
            if (!data || !data.responseData) {
                return keratin_callback(elem, null);
            }
            return keratin_callback(elem, data.responseData.feed);
        }
    });
}

$(document).ready(function() {
    keratin($('#da_gallery'), 'http://backend.deviantart.com/rss.xml?q=gallery%3Adeusuk%2F28671222&type=deviation')
});

此处详细信息:http://www.adamjamesnaylor.com/2012/11/05/Keratin-DeviantART-Latest-Deviation-Widget.aspx