RSS談

          Posted on 2007-12-07 21:32 yukui 閱讀(341) 評論(0)  編輯  收藏 所屬分類: 技術

          一、基本概念:

          “忽如一夜春風來,千樹萬樹梨花開”,近來RSS的概念已然流行開來,那么,什么才是RSS呢?怎樣才能實現RSS的功能呢?以下是手藝人對RSS的理解。

          RSS的概念:

          以下是某知名網站對RSS的標準描述:
          RSS是站點用來和其他站點之間共享內容的一種簡易方式(也叫聚合內容),通常被用于新聞和其他按順序排列的網站,例如Blog。一段項目的介紹可能包含新聞的全部介紹等。或者僅僅是額外的內容或者簡短的介紹。這些項目的鏈接通常都能鏈接到全部的內容。網絡用戶可以在客戶端借助于支持RSS的新聞聚合工具軟件,在不打開網站內容頁面的情況下閱讀支持RSS輸出的網站內容。說得更加簡單一點,RSS就是一種用來分發和匯集網頁內容的XML格式!

          注意最后一句,明白了嗎?RSS實質就是一個特殊格式的XML文件

          RSS的過程:
          1、服務器提供RSS服務,實際上就是生成一個RSS格式的XML文件。
          2、RSS閱讀器接收這個RSS格式的XML文件并解釋他的內容,然后將他顯示出來。其顯示的內容相當于一個目錄表,這個目錄表中包含有到原文的超級鏈接。
          3、用戶點擊RSS文件中的鏈接,瀏覽器顯示出全文。

          如果你用過RSS,你會發現他實際上不過是一個導航的目錄表而已,你訂閱了一個RSS,其實質就是從網站上接收了一個RSS格式的XML文件,然后,你的RSS閱讀器就解釋這個XML文件,然后(總是有然后)顯示出來,當然,在這個過程中,閱讀器也許會將其轉換為HTML格式文件(如Maxsthon就是這樣處理的。)說到底,XML不過是一種保存數據的方式,對數據含義的解釋和使用最終都是應用軟件的事。


          二、RSS的文件格式:

          從上文可以看出,RSS技術的關鍵在于RSS格式的XML文件,正是這個XML文件溝通了服務器端和客戶機端,S和C端通過這個XML文件進行對話:S端按固定的格式生成XML文檔,C端接收這個文檔并解釋之,然后顯示出來。

          RSS具有固定的格式,當然,由于歷史和版本的原因,RSS的XML文件格式具有好幾種,以下是常見的幾種:

          Example  Of RSS

          Figure 1 Sample RSS 1.0 Document

          <rdf:RDF
            xmlns:rdf="   xmlns="   xmlns:dc=" >
            <channel rdf:about="
              <title>The XML Files</title>
              <link>http://skonnard.com/blog</link>
              <description>by Aaron Skonnard</description>
              <image rdf:resource="
              <items>
                <rdf:Seq>
                  <rdf:li resource="
                  <rdf:li resource="       </rdf:Seq>
              </items>
            </channel>
            <image rdf:about="
              <title>skonnard.com</title>
              <link>http://skonnard.com/blog</link>
              <url>http://skonnard.com/blog/images/image.gif</url>
            </image>
            <item rdf:about="
              <title>1st blog entry</title>
              <link>http://skonnard.com/blog/entry1</link>
              <description>This is my first blog entry.</description>
              <dc:date>2004-01-13T17:16:44.9803903-07:00</dc:date>
            </item>
            <item rdf:about="
              <title>2nd Blog Entry</title>
              <link>http://skonnard.com/blog/entry1</link>
              <description>This is my second blog entry.</description>
              <dc:date>2004-01-13T17:16:45.9803903-07:00</dc:date>
            </item>
          </rdf:RDF>


          --------------------------------------------------------------------------------
          Figure 2 Sample RSS 2.0 Document

          <rss version="2.0">
            <channel>
              <title>The XML Files</title>
              <link>http://Skonnard.com/blog</link>
              <description>by Aaron Skonnard</description>
              <image>
                <url>http://skonnard.com/blog/images/image.gif</url>
                <title>skonnard.com</title>
                <link>http://skonnard.com/blog/</link>
              </image>
              <item>         
                <title>1st blog entry</title>
                <link>http://skonnard.com/blog/entry1</link>
                <description>This is my first blog entry.</description>
                <pubDate>Wed, 14 Jan 2004 17:16:44 GMT</pubDate>
              </item>
              <item>
                <title>2nd blog entry</title>
                <link>http://skonnard.com/blog/entry1</link>
                <description>This is my second blog entry</description>
                <pubDate>Wed, 14 Jan 2004 17:16:45 GMT</pubDate>
              </item>
            </channel>
          </rss>


          --------------------------------------------------------------------------------
          Figure 3 Sample Atom 0.3 Feed

          <feed version="0.3" xml:lang="en-us"
            xmlns="
          >
            <title>The XML Files</title>
            <link>http://skonnard.com/blog/</link>
            <modified>2004-01-13T17:16:45.0004199-07:00</modified>
            <tagline>by Aaron Skonnard</tagline>
            <author>
              <name>Aaron Skonnard</name>
            </author>
            <entry>
              <title>1st blog entry</title>
              <link>http://skonnard.com/blog/entry1</link>
              <created>2004-01-13T17:16:44.9803903-07:00</created>
              <content type="text/html" mode="xml">
                <body xmlns="
                  <p>This is my first blog entry</p>
                </body>
              </content>
            </entry>
            <entry>
              <title>2nd blog entry</title>
              <link>http://skonnard.com/blog/entry2</link>
              <created>2004-01-13T17:16:45.9803903-07:00</created>
              <content type="text/html" mode="xml">
                <body xmlns="
                  <p>This is my second blog entry</p>
                </body>
              </content>
            </entry>
          </feed>


          --------------------------------------------------------------------------------
          Figure 4 Sample Blogroll (OPML)

          <opml>
            <head> <title>Aaron's Favorite Blogs</title> </head>
            <body>
              <outline type="rss" title="PDC Bloggers"
                description="PDC Bloggers website"
                xmlUrl="
                htmlUrl="     <outline type="rss" title="MSDN Magazine: Current Issue"
                description="The Microsoft Journal for Developers"
                xmlUrl="
                htmlUrl="     <outline type="rss" title="MSDN Just Published"
                description="Keep current ..."
                xmlUrl="
                htmlUrl="   </body>
          </opml>


          --------------------------------------------------------------------------------
          Figure 5 Generating an RSS 2.0 Feed in ASP.NET

          <%@ Page language="c#" Codebehind="rss.aspx.cs"
              AutoEventWireup="false" Inherits="SimpleBlog.rss" %>
          <rss version="2.0">
            <channel>
              <title>My Blog</title>       
              <link>http://localhost/simpleblog/default.aspx</link>
              <description>A weblog about nothing...</description>
              <language>en-us</language>
              <asp:Repeater id="Items" runat="server">
                <ItemTemplate>
                  <item>
                  <title><%#DataBinder.Eval(Container.DataItem,
                      "title")%></title>
                  <description><%#DataBinder.Eval(
                      Container.DataItem,"description")%></description>
                  <pubDate><%#DataBinder.Eval(Container.DataItem,
                      "pubdate") %></pubDate>
                  <link><%# DataBinder.Eval(Container.DataItem, "link") %></link>
                  </item>
                </ItemTemplate>
              </asp:Repeater>
            </channel>
          </rss>


          --------------------------------------------------------------------------------
          Figure 6 RSS Aggregator Web User Control

          <%@ Control Language="c#" AutoEventWireup="true"
              EnableViewState="false" Debug="true"%>
          <%@ Import namespace="System.Xml" %>
          <%@ OutputCache Duration="1800" VaryByParam="none" %>
          <script runat="server" language="C#">
          private void Page_Load(object sender, System.EventArgs e)
          {
            StringBuilder sb = new StringBuilder();
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("blogroll.opml"));
            int numToDisp = int.Parse(doc.SelectSingleNode(
              "
          /opml/@numberToDisplay").InnerText);
            XmlNodeList rss = doc.SelectNodes("//outline/@xmlUrl");
            foreach (XmlNode r in rss)
            {
              XmlDocument blogdoc = new XmlDocument();
              blogdoc.Load(r.Value);
              XmlNodeList items = blogdoc.SelectNodes("http://item"); 
              for (int i=0; i<items.Count && i<numToDisp; i++)
              {
                string author="";
                XmlNode authorNode = items[i].SelectSingleNode(
                 "*[local-name()='author' or local-name()='creator']");
                if (authorNode != null) author = authorNode.InnerText;
                sb.Append(String.Format(
                  "•&nbsp;<a href={0}>{1} ({2})</a><br/>",
                  items[i].SelectSingleNode("link").InnerText,
                  items[i].SelectSingleNode("title").InnerText, author));
              }       
            }
            EntriesHTML.Text = sb.ToString();
          }
          </script>
          <style> <!-- styles omitted for brevity --> ...  </style>

          <div class="title">UNUG Blogs</div>
          <asp:Literal id="EntriesHTML" runat="server"></asp:Literal>

           
          以上RSS格式的主要區別在于1.0版RSS和2.0版RSS的區別:
          RSS 1.0 和 2.0 格式所包含的核心信息相同,但其結構不一樣。我提供了一個 RSS 1.0 文檔及一個同等的 RSS 2.0用于對照。
          你會注意到頂行右邊開始的根元素的差異,RSS 1.0 的根元素是 rdf:RDF,而 RSS 2.0 的根元素是 rss。rss 還包含一個強制版本屬性用以表示所用的RSS的準確格式(可能的值包括:0.91, 0.94 等)。另一個主要差別是 RSS 1.0 文檔有名字空間限定,RSS 2.0 的文檔就沒有。不管怎樣,包含在兩個文檔中的信息本質上是一樣的。
            兩個版本都包含 channel 元素,而 channel 元素又包含三個必須的元素:title、description 和 link,其代碼如下:
          <channel>
             <title><!-- channel 的標題 --></title>
             <description><!-- 簡要描述 --></description>
             <link><!-- channel 的 URL --></link>
             <!-- 可選/可擴展元素 -->
          </channel>     
            除了這些必須的元素外,RSS 1.0 還定義了三個附加元素:image、items 和 textinput,其中,image 和 textinput 是可選的。另一方面,RSS 2.0 提供了 16 個附加元素,其中也包括 image、items 和 textinput,此外還有 language、copyright、managingEditor、pubDate 和 category。RSS 1.0 允許通過定義在單獨的 XML 名字空間中的可擴展元素來創建這種類型的元數據。
            這兩種格式在結構上的主要區別必須要看其 item、image 和 textinput 節點的表示形式。RSS 1.0 中,channel 元素包含對 item、image 和 textinput 節點的引用,這些節點存在于 channel 節點本身之外。這樣在 channel 和 所引用的節點之間建立了一種 RDF 關聯。如 Figure 1 所示,channel 元素與一個 image 元素以及兩個 item 元素關聯。RSS 2.0 中,item 元素只是在 channel 元素中連續排放(如 Figure 2 所示)。item 元素包含實際的新聞項信息。item 的結構在兩個版本中是相同的。item 元素通常包含 title、link 和 description 元素,如下代碼所示:

          <item>
             <title><!-- 項標題 --></title>
             <link><!-- 項 URL --></link>
             <description><!-- 簡要描述 --></description>
             <!-- 可選的/可擴展的元素 -->
          </item>
            在 RSS 1.0 中,title 和 link 是必須的,description 是可選的。而在 RSS 2.0 中,title 或 description 必須提供其中的一個;其它均可選。這些只是定義在 RSS 1.0 中的 item 元素。RSS 2.0 提供幾個其它可選元素,其中有 author、category、comments、enclosure、guid、pubDate 和 source。RSS 1.0 獲取這樣的元數據是通過定義在單獨的 XML 名字空間中稱為 RSS 模塊的可擴展元素來實現的。例如,在 1中,item 的日期是用 Dublic Core 模塊的 <dc:date> 元素表示的。
          三、知道這些,我們能夠做什么?

          現在,你知道RSS是怎么回事了嗎?其實當你理解了RSS,你會覺得他非常簡單:
          知道了這些內容,我們至少可以做兩方面的事:
          1、按照上面h提供的RSS文檔格式編寫一個XML的文檔xxx.xml,將這個文檔放到你的網站上,
          打開你的rss閱讀器,將這個xml的URL地址輸入RSS訂閱的URL欄,然后點擊“確定”,你看到了什么?你的RSS閱讀器中已經顯示出了你所編輯的內容。
          原來你已經提供了RSS服務,一切就是這樣簡單,下面的內容就是怎樣將你網站的內容制成RSS格式的XML文件了,在這個過程中你會用上一些技術,這不屬于本文的范疇。
          2、編寫一個html文檔,<body>和</body>之間寫上一個<body onload="initrss();"><div id="RSSDIV"></div></body>,
          然后,寫一段javascript程序的函數initrss(),這個函數將從你的網站上接收xml文檔(有多種方法,如使用xmldoc技術就行),然后解釋這個xml文檔,最簡單也是最基本的,你須要析出文檔名(即item中的title項)和他的鏈接(即item中的link項), 然后為這個文檔名加上超級鏈接:htmlcode=<a href="item的link項">item的link項</a>,最后,當然會有最后,不是嗎?將這一段html代碼寫入一個<div></div>中:RSSDIV.innerHTML=htmlcode;,保存這個html文檔,用你的IE瀏覽器打開它,你看到了什么?你已經制成了一個簡單的rss閱讀器了,就這樣簡單嗎?是的,一切就這樣簡單。而簡單總是美好的。


          posts - 131, comments - 12, trackbacks - 0, articles - 32

          Copyright © yukui

          主站蜘蛛池模板: 东平县| 读书| 宁德市| 隆昌县| 松滋市| 白朗县| 太仆寺旗| 清新县| 涟源市| 渝北区| 和政县| 环江| 潞西市| 清原| 威远县| 浦江县| 衡水市| 北流市| 南安市| 迭部县| 乌拉特后旗| 安达市| 麟游县| 高淳县| 商城县| 宣武区| 文登市| 类乌齐县| 新蔡县| 大渡口区| 吉木乃县| 辛集市| 成安县| 商水县| 大英县| 平顶山市| 岑溪市| 安阳县| 吴江市| 柳林县| 沙河市|