Thứ Tư, 13 tháng 5, 2009

RSS/Atom và cách tạo RSS/Atom cho website của bạn

Ngày nay RSS và Atom đã trở nên phổ biến, tuy nhiên sẽ có nhiều lập trình viên đặt câu hỏi: Làm thế nào để tạo ra RSS cho website của bạn. Là một lập trình viên tôi cũng đã từng đặt câu hỏi trên. Các bạn có thể tìm thấy bài viết "Cách Tạo RSS trong ASP.NET" trên web. Tuy nhiên, chúng ta có thể tạo RSS bằng nhiều cách khách nhau nêu phân tích sâu cấu trúc của RSS: Ví dụ như sau:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <rss version="2.0">
   3:   <channel>
   4:     <title>RSS2.0Example</title>
   5:     <link>http://www.exampleurl.com/example/index.html</link>
   6:     <description>This is an example RSS 2.0 feed</description>
   7:     <language>en-gb</language>
   8:     <copyright>Copyright 2002, Oreilly and Associates.</copyright>
   9:     <managingEditor>example@exampleurl.com</managingEditor>
  10:     <webMaster>webmaster@exampleurl.com</webMaster>
  11:     <rating> </rating>
  12:     <pubDate>03 Apr 02 1500 GMT</pubDate>
  13:     <lastBuildDate>03 Apr 02 1500 GMT</lastBuildDate>
  14:     <docs>http://backend.userland.com/rss</docs>
  15:     <skipDays>
  16:       <day>Monday</day>
  17:     </skipDays>
  18:     <skipHours>
  19:       <hour>20</hour>
  20:     </skipHours>
  21:     <category  domain="http://www.dmoz.org">
  22:       Business/Industries/Publishing/Publishers/
  23:       Nonfiction/Business/O'Reilly_and_Associates/
  24:     </category>
  25:     <generator>NewsAggregator'o'Matic</generator>
  26:     <ttl>30</ttl>
  27:         <cloud domain="http://www.exampleurl.com" port="80" path="/RPC2"
  28:       registerProcedure="pleaseNotify" protocol="XML-RPC" />
  29:         <image>
  30:           <title>RSS2.0 Example</title>
  31:           <url>http://www.exampleurl.com/example/images/logo.gif</url>
  32:           <link>http://www.exampleurl.com/example/index.html</link>
  33:           <width>88</width>
  34:           <height>31</height>
  35:           <description>The World's Leading Technical Publisher</description>
  36:         </image>
  37:         <textInput>
  38:           <title>Search</title>
  39:           <description>Search the Archives</description>
  40:           <name>query</name>
  41:           <link>http://www.exampleurl.com/example/search.cgi</link>
  42:         </textInput>
  43:         <item>
  44:           <title>The First Item</title>
  45:           <link>http://www.exampleurl.com/example/001.html</link>
  46:           <description>This is the first item.</description>
  47:           <source url="Another">http://www.anothersite.com/index.xml">Another Site</source>
  48:           <enclosure url="http://www.exampleurl.com/example/001.mp3" length="543210" type="audio/mpeg"/>
  49:           <category domain="http://www.dmoz.org">
  50:             Business/Industries/Publishing/Publishers/
  51:             Nonfiction/Business/O'Reilly_and_Associates/
  52:           </category>
  53:           <comments>http://www.exampleurl.com/comments/001.html</comments>
  54:           <author>Ben Hammersley</author>
  55:           <pubDate>Sat, 01 Jan 2002 0:00:01 GMT</pubDate>
  56:           <guid isPermaLink="true">http://www.exampleurl.com/example/001.html</guid>
  57:         </item>
  58:         <item>
  59:           <title>The Second Item</title>
  60:           <link>http://www.exampleurl.com/example/002.html</link>
  61:           <description>This is the second item.</description>
  62:           <source url="Another">http://www.anothersite.com/index.xml">Another Site</source>
  63:           <enclosure url="http://www.exampleurl.com/example/002.mp3" length="543210" type="audio/mpeg"/>
  64:           <category domain="http://www.dmoz.org">
  65:             Business/Industries/Publishing/Publishers/
  66:             Nonfiction/Business/O'Reilly_and_Associates/
  67:           </category>
  68:           <comments>http://www.exampleurl.com/comments/002.html</comments>
  69:           <author>Ben Hammersley</author>
  70:           <pubDate>Sun, 02 Jan 2002 0:00:01 GMT</pubDate>
  71:           <guid isPermaLink="true">http://www.exampleurl.com/example/002.html</guid>
  72:         </item>
  73:         <item>
  74:           <title>The Third Item</title>
  75:           <link>http://www.exampleurl.com/example/003.html</link>
  76:           <description>This is the third item.</description>
  77:           <source url="Another">http://www.anothersite.com/index.xml">Another Site</source>
  78:           <enclosure url="http://www.exampleurl.com/example/003.mp3" length="543210" type="audio/mpeg"/>
  79:           <category domain="http://www.dmoz.org">
  80:             Business/Industries/Publishing/Publishers/
  81:             Nonfiction/Business/O'Reilly_and_Associates/
  82:           </category>
  83:           <comments>http://www.exampleurl.com/comments/003.html</comments>
  84:           <author>Ben Hammersley</author>
  85:           <pubDate>Mon, 03 Jan 2002 0:00:01 GMT</pubDate>
  86:           <guid isPermaLink="true">http://www.exampleurl.com/example/003.html</guid>
  87:         </item>
  88:       </channel>
  89: </rss>


Đây là một ví dụ về một file chứa RSS. Với ví dụ này chúng ta thấy, việc tạo RSS bản chất là tạo một tài liệu XML. Và việc sinh ra file XML này có rất nhiều cách khác nhau như: xml, javascrip, C#...Với sự hỗ trợ của LINQ ngày nay, thì việc tạo XML file càng trở nên đơn giản hơn nhiều.

Không có nhận xét nào:

Đăng nhận xét