2009-11-05 27 views
0

我有一些xml在服務器上(http://server.com/my.xml)。這裏是例子:Ruby(RoR)XML哈希,然後顯示在視圖

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE current PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<current> 
    <song>Bruce - Inside The Machine</song> 
    <song>02 Duel of the Fates 1</song> 
</current> 

on Rails應用程序我做的:

response = open("http://server.com/my.xml").read 
@sngs = Hash.from_xml(response) 

現在,在視圖中,我希望把每一個「歌」的值在例如「P」的標籤,但一個接一個。我必須把例如確切的第一或第二。

怎麼可以做到?

(很多,非常感謝!)

回答

1

Hash.from_xml將創建形式的哈希:

{"current" => {"song" => ["Bruce - Inside The Machine", "02 Duel Of the Fates 1"]}} 

我不能完全確定要顯示什麼,而是你可以訪問個人使用歌曲:

@sngs["current"]["song"][0] 

如果你想顯示p標籤內的所有歌曲,例如,你可以這樣做:

<%- @sngs["current"]["song"].each do |song| %> 
    <p><%= song %></p> 
<%- end %> 
+0

謝謝!這正是我需要的!謝謝格雷格! – 2009-11-05 23:02:40