Ruby (RoR) XML to hash and then show in views

I have some xml on server (http://server.com/my.xml). Here is the example:

<?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 application I do:

response = open(https://www.e-learn.cn/content/wangluowenzhang/"http://server.com/my.xml").read @sngs = Hash.from_xml(response)

Now, in Views, I want to put each "song" value in "p" tag for example, but not one by one. I have to put, for example exact first or second.

How can it can be done?

(Many, many thanks!)

Answer1:

Hash.from_xml will create a Hash of the form:

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

I'm not entirely sure what you want to display, but you can access individual songs using:

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

If you wanted to display all the songs inside p tags, for instance, you could do:

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

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Ruby (RoR) XML to hash and then show in views