Ruby/Rails - Parsing XML using nokogiri and splitting a node value?

Since this is an RSS feed, you may want to consider an RSS parser.

Since this is an RSS feed, you may want to consider an RSS parser: require 'simple-rss' require 'open-uri' feed = 'enhancetv.com.au/tvguide/rss/melbourners... rss = SimpleRSS. Parse open(feed) rss.items. Each do |item| puts item.

Title, item. Link, item. Description end.

For the example title string you mentioned: DateTime. Parse(s. Split(" - ")-2..-1.

Join(" ")) This gets you a DateTime object: Wed, 21 Dec 2011 07:00:00 +0000 But you have to keep an eye on the title variations you might need to deal with. Modify the split a bit to meet your need. Update: didn't noticed you also want more info on how to parse the document.So here's how: doc = Nokogiri::XML(open("enhancetv.com.au/tvguide/rss/melbourners...)) data = doc.

Xpath("//item"). Map do |item| item. Search("title").first.

Content, item. Search("link").first. Content, item.

Search("description").first. Content end This will load all title, link and description for items in the data array. Nokogiri::XML accepts a string as xml document content, so you need to open the url then feed the result to it.

Def parse_time(text) items = text. Split("-") DateTime. Strptime("#{items-2.

Strip}#{items-1. Strip}", "%H:%M:%S%d/%m/%Y") end content = Net::HTTP. Get(URI.

Parse("enhancetv.com.au/tvguide/rss/melbourners...)) doc = Nokogiri::XML(content){|config| config. Noblanks } doc. Search("//item").

Map{ |node| node.children. Inject({}) do |hash, node| if node. Name == "title" #or another name hash"created_at" = parse_time(node.

Text) end hashnode. Name = node. Text hash end }.

Mm, how would I further retrieve description, link, etc? – Fulvio Dec 21 at 2:31 This code only shows how to extract the node but I actually need the rest of the nodes as well. Ideally I'd like to store all the values in an object to use later. – Fulvio Dec 21 at 2:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions