开发者

google feed api and grabbing tags and attributes

var feedcontainer=document.getElementById("feeddiv")
var feedurl="http://example.com"
var feedlimit=5
var rssoutput="<b>Latest Slashdot News:</b><br /><ul>"

function rssfeedsetup(){
var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method
feedpointer.setNumEntries(feedlimit) //Google Feed API method
feedpointer.load(displayfeed) //Google Feed API method
}

function displayfeed(result){
if (!result.error){
var thefeeds=result.feed.entries
for (var i=0; i<开发者_运维百科thefeeds.length; i++)
rssoutput+="<li><a href='" + thefeeds[i].link + "'>" + thefeeds[i].title + "</a></li>"
rssoutput+="</ul>"
feedcontainer.innerHTML=rssoutput
}
else
alert("Error fetching feeds!")
}

window.onload=function(){
rssfeedsetup()
}

This code I grabbed from a tutorial, but it only grabs the items specified in the Google documentation, which are the basics like link and title.

In my rss feed, I have a tag that resides in the Is it possible to get at that?


Heres an example for retrieving the url of an image from the enclosure element of a feed element. The enclosure element is not included in the standard json format, so to access it, the resultFormat must be set to MIXED_MODE. the result entries will now, in addition to json, include a xmlNode from which the enclosure element can be retrieved, and the url extracted:

var feed = new google.feeds.Feed(feedUrl);
feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
feed.load( function(result) {
   var entry = result.feed.entries[i];
   var entryImageUrl = entry.xmlNode.getElementsByTagName("enclosure")[0].getAttribute("url");
});


I figure it out. You have to set the feed to bring back XML using feed.setResultFormat(google.feeds.Feed.XML_FORMAT);

then if you want to grab anything you use

item.getElementsByTagName('enclosure')[0].getAttribute("url");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜