On twitter

@Lydia618 相差不遠了 (茶)

follow me on twitter

用 Simplepie Live 讀取 RSS 2

昨天在弄我網站的首頁, 想說做個單純的 HTML, 不過想要在上面放個部落格最新文章的列表, 所以就想說用 Simplepie Live 來玩玩看, 就寫了下面那一段。

首先要使用 Simplepie Live, 只要先放進他所提供的 javascript。

<script type="text/javascript" src="http://live.simplepie.org/app/0.5/base.js"></script>

然後就有 Simplepie Live 的物件可以用來讀取 RSS 囉, 範例如下方。

先說明一下下面這一段… 基本上, 就是網頁裡先有一個空的 ul tag, id 為 rss, 然後就用 Simplepie Live 把 RSS 文章的標題跟連結都包進一個 li tag, 然後放進 ul, 這樣。

window.onload = function() {
	var url = 'http://feeds.feedburner.com/tzangms';

	var feed = new SimplePie(url, {
		onSuccess: function(feed) {
			var rss = document.getElementById('rss');
			for (var i in feed.items) {
				var title = feed.items[i].title;
				var permalink = feed.items[i].permalink

				var text = document.createTextNode(title);

				var a = document.createElement('a');
				a.setAttribute('href', permalink);
				a.appendChild(text);

				var li = document.createElement('li');
				li.appendChild(a);

				rss.appendChild(li);
			}
		}
	});
}

不過, 其實用 Google AJAX Feed API 好像還比較快 XD

December 7th, 2008 Programming Tags: ,

2 Responses to “用 Simplepie Live 讀取 RSS”

  1. [...] 用 Simplepie Live 讀取 RSS [...]

  2. [...] Oceanic | 人生海海 » 用 Simplepie Live 讀取 RSS (tags: feed rss rsstools feedtools) [...]

Leave a Reply

Partners of Oceanic / 人生海海

jiwosca