tags:

views:

29

answers:

2

I am using the following code to get the Item from the Rss feed but it only gives me the first item. How can I get all the items:

root.elements["channel/item"].each do |item|

    titles << item.text 

  end

UPDATED:

titles = [] 

  # go through the collection and prints the title

  root.elements["channel/item"].each do |item|

    titles << item.text 

  end

  puts titles
A: 

There's nothing wrong with that code. Ensure you properly set titles = [] before trying to append elements. Also you should provide more details about which library you are using. I assume this is REXML, but I'm not sure.

EDIT: I just noticed you call elements[]. Change the code to

root.elements.each("channel/item") do |item|
  titles << item.text 
end
Simone Carletti
That did not print anything!!
See my edit, use the .each(path) method call.
Simone Carletti
A: 

Here is the code that worked for me:

root.elements.each("channel/item") do |item|

    #puts item

    titles << item.text("title")

  end

  puts titles