現在我們來介紹一下怎麼讀取其內用~
在們現在先將index Action改成:
def index @xml = Net::HTTP.get(URI.parse('http://www.simplexteam.org/index.php?format=feed&type=rss')) begin @rss = REXML::Document.new(@xml) flash[:notice] = "XML格式正確" rescue REXML::ParseException flash[:notice] = "XML格式錯誤" end end接著把index View改成:
<h1>Xml#index</h1> <div><%= flash[:notice] %></div> <% @rss.root.each_element do |channel| %> Channel <% channel.each_element do |channel_child| %> <%= "#{channel_child.name}: #{channel_child.text}" %> <% channel_child.each_element do |item_child| %> <%= "#{item_child.name}: #{item_child.text}" %> <% end if channel_child.name == "item" %> <% end %> <% end %>這樣便可列出XML的內容(本XML是讀取本站的RSS)
each_element能列舉其包含的element,你如想抓取某特定的elements,可以這樣:
channel.elements["description"]這樣便是抓取名為description的element~
方便吧^^
| < 上一頁 | 下一頁 > |
|---|



