RSS 是一種“輕量級(jí)、多用途、可擴(kuò)展的元數(shù)據(jù)描述及聯(lián)合推廣格式”,也可以理解為一種規(guī)范。它本身是一種 XML 格式,用于為內(nèi)容整合客戶端提供選擇性的、匯總過的 ;Web 內(nèi)容。如今,許多站點(diǎn)都開始通過創(chuàng)建 RSS feed 向?yàn)g覽者提供內(nèi)容整合服務(wù),提供新聞、站點(diǎn)內(nèi)容的更新等等。瀏覽者可以通過一些客戶端軟件方便地得到這些組織、匯總了的信息。
那么,怎么在我們自己的網(wǎng)站上創(chuàng)建 RSS feed 呢?下面我以 Asp+Access 為例介紹一下。
既然 RSS 是一種 XML 格式的文檔,那么我們就應(yīng)當(dāng)可以對(duì)后臺(tái)數(shù)據(jù)庫(kù)中的數(shù)據(jù)按照條件進(jìn)行篩選、組織,然后通過 ASP生成 XML 格式的數(shù)據(jù)流,最后發(fā)送到客戶端進(jìn)行瀏覽。
數(shù)據(jù)的選取、匯集是 Asp 的拿手好戲,關(guān)鍵在于怎么生成 XML 格式的數(shù)據(jù)流。其實(shí) Asp 也已經(jīng)有自己的解決辦法,就是在寫數(shù)據(jù)前對(duì) response 對(duì)象的ContentType 屬性進(jìn)行定義。如果 response.ContentType 的取值為 "text/xml",就會(huì)向?yàn)g覽者發(fā)送 XML 格式的數(shù)據(jù)流。
在 IE 瀏覽器中調(diào)用 RSS 源的方法和普通的鏈接沒有什么區(qū)別,格式是:
a type="application/rss+xml" href="RssFeed.asp">RSS說明/a>
其中 type="application/rss+xml" 加不加好象沒有什么區(qū)別。
以下程序段是創(chuàng)建我的網(wǎng)站“十萬(wàn)個(gè)為什么”(http://www.why100000.com/)上的“技術(shù)新聞”欄目的 RSS feed 的源代碼
,文件名為 RssFeed_news.asp。
其中,變量 sXmlClear 用于聲明產(chǎn)生的文檔是一段 XML 格式的文檔,該聲明是可選的,以保持與舊版本 XML 的向后兼容
性。
sRssHead 定義 Rss 的基本元素。RSS feed 通常由 4 個(gè)主要元素構(gòu)成:channel>,l t;image>,item> 和
textinput>。其中,channel> 元素是必需的,item> 元素至少要出現(xiàn)一次。textinput> 和 image> 元素是可選的,
是否使用要視具體情況而定。
channel> 元素包含 Channel(RSS feed 的來源)的一個(gè)簡(jiǎn)單描述。title> 是頻道的名稱/標(biāo)題;link> 是與頻道內(nèi)容對(duì)
應(yīng)的包含了完整內(nèi)容的那個(gè)網(wǎng)頁(yè)的 URL;description> 是與 channel> 的內(nèi)容有關(guān)的簡(jiǎn)單描述;language> 代表語(yǔ)言。
還有一些別的屬性,不是太常用。
item> 元素用于對(duì)數(shù)據(jù)庫(kù)中的記錄進(jìn)行描述。item> 一般有若干項(xiàng),對(duì)應(yīng)了一個(gè) Rss feed 的數(shù)據(jù)集合。
復(fù)制代碼 代碼如下:
!-Filename:RssFeed_news.asp:-->
% Option explicit %>
!-- #include file="./conn.inc" -->
%
Dim sSQL, rs, sCrLf, sXmlClear, sRssHead, sRssEnd
sCrLf = chr(13) chr(10) '回車+換行
sXmlClear = "?xml version='1.0' encoding='gb2312'?>" sCrLf
sRssHead = "rss version='2.0'>" sCrLf
sRssHead = sRssHead "channel>" sCrLf
sRssHead = sRssHead "title> Why100000 /title>" sCrLf
sRssHead = sRssHead "description> Why100000 /description>" sCrLf
sRssHead = sRssHead "link>http://news.why100000.com/;/link>" sCrLf
sRssHead = sRssHead "language>zh-cn/language>" sCrLf
sRssHead = sRssHead "docs>Why100000.COM News Center/docs>" sCrLf
sRssHead = sRssHead "generator>Rss Generator By WWW.Why100000.COM/generator>" sCrLf
sRssEnd = "/channel>/rss>"
Response.CharSet="gb2312" '數(shù)據(jù)集
Response.ContentType="text/xml" '數(shù)據(jù)流格式定義
'輸出:
Response.write sXmlClear
Response.write sRssHead
sSQL="select top 15 * from news order by sortid desc"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sSQL, s_Conn, 1, 1
if not (rs.eof and rs.bof) then
do while not rs.eof
response.write "item>" sCrLf
response.write "title> " rs("f_topic") " /title>" sCrLf
response.write "link> " "http://www.why100000.com/_news/show_a_new.asp?autoid=";
rs("f_i_autoid") " /link>" sCrLf
response.write "author> " rs("f_author") " /author>" sCrLf
response.write "pubDate> " rs("f_datetime") " /pubDate>" sCrLf
response.write "/item>" sCrLf sCrLf
rs.movenext
loop
end if
rs.close
set rs=nothing
Response.write sRssEnd
%>
IE 中的調(diào)用格式是:a ;>技術(shù)新聞
RSS/a>。如果用一些客戶端軟件訂閱該 RSS,訂閱的 Url 就是http://www.why100000.com/_news/RssFeed_news.asp。