解析RSS命名空间媒体:缩略图(js / jQuery)

时间:2015-03-09 16:07:33

标签: javascript rss

我一直试图这样做几天,而且我试图弄清楚如何做到这一点。

以下是我正在尝试阅读的Atom Feed中的条目示例。

<entry>
            <title>Is Traffic Draining Employee Productivity at Your Workplace?</title>
            <author><name>Seth Stuck</name></author>
            <source>
                <title>Team Georgia</title>
                <id>http://team.georgia.gov/feed/</id>
                <updated>2015-03-03T19:19:13.763475Z</updated>
            </source>
            <link rel="alternate" href="http://trap.it/pxgvM" />
            <id>http://trap.it/pxgvM</id>
            <updated>2015-03-03T19:19:13.763475Z</updated>
            <published>2015-03-03T19:19:13.763475Z</published>
            <summary>Is Traffic Draining Employee Productivity at Your Workplace?</summary>
            <media:thumbnail
             xmlns:media="http://search.yahoo.com/mrss/"
             url="http://images.higgs.trap.it/sizes/orig/9fb99a95f2f04977a0994c1a9f1d32cd.png"  />
        </entry>

我想抓住的唯一非常重要的项目是

<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://images.higgs.trap.it/sizes/orig/9fb99a95f2f04977a0994c1a9f1d32cd.png"  />

我找不到插件或有帮助的文章。我从我读过的所有内容中得到的最接近的是:

$(document).ready(function() {

    var getXML = function() {
        var output = $('#output');
        var html = '';
        output.empty();

        $.ajax({
            type: 'GET',
            dataType: 'xml',
            data: null,
            url: 'file.xml',
            success: function(xml) {
                var $entries = $(xml).find('entry');
                $entries.each(function() {
                    var $entry = $(this);
                    var entry = $entry[0]; // DOM element
                    var content = $entry.find('content').text();
                    var thumbnail = entry.getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'thumbnail')[0].getAttribute('url');
                    html += '<img src="' + thumbnail + '" />';
                    html += content;
                });
                output.html(html);
            },
            error: function(xhr, status, text) {
                console.log(status + ': ' + text);
            }
        });

    };

    $('#action').on('click', getXML);

});

哪来自http://gabrieleromanato.name/jquery-parsing-XML-with-namespaces-the-definitive-solution/

我无法在我的情况下工作。有没有人有更多的建议?

由于

2 个答案:

答案 0 :(得分:0)

目前Team Georgia feed没有任何media:thumbnail条目,因此无法用于测试,但以下是另一个Feed的示例:

    <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("feeds", "1");

    function initialize() {
        var feed = new google.feeds.Feed('http://channel9.msdn.com/Feeds/RSS'); // change the feed url 
        feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
        feed.setNumEntries(25);
        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 mediaEntries = entry.xmlNode.getElementsByTagNameNS('*','thumbnail');
            for (var j = 0; j < mediaEntries.length; j++) {
                var mediaEntry = mediaEntries[j]; // This is the media:thumbnail item
                var mediaThumbnailUrl = mediaEntry.attributes.getNamedItem('url').value // Just the url

                var imageElement = document.createElement("img");
                imageElement.setAttribute('src', mediaThumbnailUrl);
                imageElement.setAttribute('width', '200px');
                imageElement.setAttribute('height', '200px');
                var div = document.createElement("div");
                div.appendChild(document.createTextNode(entry.title));
                div.appendChild(imageElement);
                container.appendChild(div);
            }
          }
        }
      });
    }
    google.setOnLoadCallback(initialize);
    </script>
    </head>
    <body><div id="feed"></div></body

我正在使用Google Feed API,但循环xml标记的部分应该可以使用其他加载Feed的方法。类似的问题:Google Feed API - returning media:thumbnail

答案 1 :(得分:0)

img_data = $(this).find("media:thumbnail").eq(1).attr("url");

在媒体之后和分号之前添加双反斜杠...