Google Feed检索图片

时间:2012-09-13 10:40:20

标签: javascript rss google-feed-api

我正在使用GoogleFeed API(https://developers.google.com/feed/)使用此代码检索RSS Feed:

    function loadAuto(){
    google.load("feeds", "1");  
    function initialize() {
        var feed = new google.feeds.Feed("http://www.automoto.sk/rss");
        feed.setNumEntries(window.localStorage.getItem("entriesNumber"));
        feed.load(function(result) {
            if (!result.error) {
                var feedlist = document.getElementById("feedAuto");
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var li = document.createElement("li");
                    var entry = result.feed.entries[i];
                    var A = document.createElement("A");
                    var descriptionSettings = window.localStorage.getItem("descriptionSettings");
                    if (descriptionSettings=="true"){
                        var h3 = document.createElement("h3");
                        var p = document.createElement("p");
                        var pDate = document.createElement("p");
                        pDate.setAttribute("style","text-align: right; margin-top: 5px;");
                        var publishedDate = new Date(entry.publishedDate);
                        publishedDateConverted = convertTime(publishedDate);
                        pDate.appendChild(document.createTextNode(publishedDateConverted));
                        h3.setAttribute("style","white-space: normal;")
                        h3.appendChild(document.createTextNode(entry.title));
                        p.setAttribute("style","white-space: normal;")
                        p.appendChild(document.createTextNode(entry.content));
                        A.setAttribute("href",entry.link);
                        A.appendChild(h3);
                        A.appendChild(p);
                        A.appendChild(pDate);
                        }
                    else{
                        A.setAttribute("href",entry.link);
                        A.appendChild(document.createTextNode(entry.title));
                    };
                    li.appendChild(A);
                    feedlist.appendChild(li);
                }
                $("#feedAuto").listview("refresh");
            }
        });
    }
    google.setOnLoadCallback(initialize);
};

一切正常但我无法加载一个elemenet。这是RSS结构:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
   <title>automoto.sk</title>
   <link>http://www.automoto.sk</link>
   <description>automoto.sk</description>   
   <language>sk</language>
   <image><title>automoto.sk</title><url>http://img.automoto.sk</url><link>http: //www.automoto.sk</link></image>
   <pubDate>Thu, 13 Sep 2012 12:22:45 +0200</pubDate>
    <item>
     <title><![CDATA[Nová Kia pro_ceed: Najdynamickejší Slovák]]></title>
     <link>http://www.automoto.sk/clanok/188988/nova-kia-pro-ceed-najdynamickejsi-slovak</link>
     <pubDate>Thu, 13 Sep 2012 09:22:00 +0200</pubDate>
     <guid isPermaLink="false">5e14ddbf095fe497eaf03f9a8cd88773</guid>
     <description><![CDATA[PARÍŽ 2012    Trojdverový variant druhej generácie typu cee’d má podstatne štipľavejší dizajn ako jeho praktickejší brat.]]></description>
     <enclosure url="http://img.automoto.sk/img/20/title/1395542-img-nova-kia-pro-ceed.jpg" length="0" type="image/jpeg"></enclosure>
    </item>
</rss>

我需要加载位于机箱中的img,但我不知道如何加载机箱url变量。也许将格式更改为XML就可以了。我已经尝试将其更改为XML但我无法使其工作。所以,如果您知道如何以JSON格式加载图像,或者如果您知道如何转换initialize()函数以便它将使用XML然后加载img URL请发布您的建议。

PS:您可以在此网站找到RSS http://www.automoto.sk/rss/

1 个答案:

答案 0 :(得分:0)

我已经使用混合格式工作了。所以我的代码改变了这样:

var feed = new google.feeds.Feed("http://www.automoto.sk/rss");
feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);

然后我补充说:

var entryImageUrl = entry.xmlNode.getElementsByTagName("enclosure")[0].getAttribute("url");

在里面循环。