Posted on 2005-11-15 14:35
橘子 閱讀(476)
評(píng)論(1) 編輯 收藏 所屬分類:
WEB開發(fā)
以下程序段是創(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 的來(lái)源)的一個(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ù)集合。
<!-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。