ITPUB??ì3
ITPUB论坛 » 动态语言 » open-uri 搭配 REXML, 轻松完成 RSS reader

新一届的微软MVP评选已经开始,欢迎各位推荐!

标题: [转载] open-uri 搭配 REXML, 轻松完成 RSS reader
离线 stone4102
follow 836


精华贴数 9
个人空间 0
技术积分 965 (1933)
社区积分 16786 (89)
注册日期 2003-10-25
论坛徽章:22
现任管理团队成员     
      

发表于 2006-5-20 17:14 
open-uri 搭配 REXML, 轻松完成 RSS reader


__________________
只看该作者    顶部
离线 stone4102
follow 836


精华贴数 9
个人空间 0
技术积分 965 (1933)
社区积分 16786 (89)
注册日期 2003-10-25
论坛徽章:22
现任管理团队成员     
      

发表于 2006-5-20 17:23 
善用open-uri 可以方便將資料從網路上下載, 加上配合 REXML 我們可以寫個簡單的 RSS reader!

open-uri 让你下载网络资料, 可以像 File 一般操作,以及支援 metadata, ex. context-type, charset…
REXML 是 XML parser
代码如下:

require 'open-uri'
require 'rexml/document'
class Rss
  attr_accessor :channel, :items

  def initialize
    @channel = @channel || {}
    @items = []
  end
  def readit(url)
    open(url) do |f|
      #puts "#{f.content_type} #{f.charset}"

      #puts f.read
      d = REXML:ocument.new(f.read)

      #
      d.elements.each("*/channel" do |e|
        @channel["title"] = e.elements["title"].text
        @channel["description"] = e.elements["description"].text
      end
      #
      d.elements.each("*/item" do |e|
        item = {}
        item["title"] = e.elements["title"].text
        item["link"] = e.elements["link"].text
        @items << item
      end
    end
  end
end

#
rss = Rss.new
rss.readit('http://www.mozilla.org/news.rdf')
puts rss.channel.inspect
rss.items.each do |i|
  puts i.inspect
end


__________________
只看该作者    顶部
离线 stone4102
follow 836


精华贴数 9
个人空间 0
技术积分 965 (1933)
社区积分 16786 (89)
注册日期 2003-10-25
论坛徽章:22
现任管理团队成员     
      

发表于 2006-5-25 10:59 
REXML 是處理 xml 的程式庫, 可以讀入 xml 檔案然後建立 DOM tree, 而且完整支援 XPath, 所以比較好用, 它是支援 unicode 的, 若要輸出 xml 還是建議用Xml Builder 比較快速



REXML
REXML Tutorial
範例
PHP code:


require 'rexml/document'



File.new(&quot;inv.xml&quot<img src="images/smilies/23.gif" smilieid="206" border="0" alt="" />

doc REXML:<img src="images/smilies/29.gif" smilieid="205" border="0" alt="" />ocument.new(f)



doc.elements.each(&quot;inventory/section&quot<img src="images/smilies/23.gif" smilieid="206" border="0" alt="" /> { |element

  
puts element.attributes[&quot;name&quot;]

}



puts

doc
.elements.each(&quot;*/section/item&quot<img src="images/smilies/23.gif" smilieid="206" border="0" alt="" /> { |element

  
puts element.elements[&quot;name&quot;].text

  puts element
.attributes[&quot;upc&quot;] 

}




__________________
只看该作者    顶部
离线 stone4102
follow 836


精华贴数 9
个人空间 0
技术积分 965 (1933)
社区积分 16786 (89)
注册日期 2003-10-25
论坛徽章:22
现任管理团队成员     
      

发表于 2006-5-25 11:00 
原始 inv.xml
PHP code:

<?xml version=&quot;1.0&quotencoding=&quot;UTF-8&quot;?>

<inventory title=&quot;OmniCorp Store #45x10^3&quot;>

  <section name=&quot;health&quot;>

    <item upc=&quot;123456789&quot; stock=&quot;12&quot;>

      <name>Invisibility Cream</name>

      <price>14.50</price>

      <description>Makes you invisible</description>

    </item>

    <item upc=&quot;445322344&quot; stock=&quot;18&quot;>

      <name>Levitation Salve</name>

      <price>23.99</price>

      <description>Levitate yourself for up to 3 hours per application</description>

    </item>

  </section>

  <section name=&quot;food&quot;>

    <item upc=&quot;485672034&quot; stock=&quot;653&quot;>

      <name>Blork and Freen Instameal</name>

      <price>4.95</price>

      <description>A tasty meal in a tablet; just add water</description>

    </item>

    <item upc=&quot;132957764&quot; stock=&quot;44&quot;>

      <name>Grob winglets</name>

      <price>3.56</price>

      <description>Tender winglets of Grob. Just add water</description>

    </item>

  </section>

</inventory>






__________________
只看该作者    顶部
离线 stone4102
follow 836


精华贴数 9
个人空间 0
技术积分 965 (1933)
社区积分 16786 (89)
注册日期 2003-10-25
论坛徽章:22
现任管理团队成员     
      

发表于 2006-5-25 11:03 

__________________
只看该作者    顶部
离线 stone4102
follow 836


精华贴数 9
个人空间 0
技术积分 965 (1933)
社区积分 16786 (89)
注册日期 2003-10-25
论坛徽章:22
现任管理团队成员     
      

发表于 2006-5-25 11:06 

__________________
只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问