Rails Hpricot gem: Unable to get the link content from xml

63 views Asked by At

I am using Hpricot gem to parse xml. I am able to get title and pubdate but it did not work for link. Here is the code snippet

items = doc.search("//item").first(6)
items.each do |item|
  feed = {}
  feed[:title] = item.search("//title").text
  feed[:link] = item.search("//link").text
  feed[:published_date] = item.search("//pubdate").text
  feeds << feed
end

The resultant hpricot elements are as follows:

#<Hpricot::Elements[{elem <item> "\n\t\t" {elem <title> "openagent.com.au" </title>} "\n\t\t" {emptyelem <link>} "http://blog.iproperty.com.au/2016/03/22/openagent-com-au/" {bogusetag </link>} "\n\t\t" {elem <comments> "http://blog.iproperty.com.au/2016/03/22/openagent-com-au/#comments" </comments>} "\n\t\t" {elem <pubdate> "Mon, 21 Mar 2016 22:43:28 +0000" </pubDate>} "\n\t\t"

I have pasted the initial part as it is the only part which is important. Can anyone tell what is the solution for it.

1

There are 1 answers

2
JohnPaul On
items = doc.search("//item").first(6)
items.each do |item|
  feed = {}
  feed[:title] = item.search("//title").text
  feed[:link] = item.search("//link").innerHTML
  feed[:published_date] = item.search("//pubdate").text
  feeds << feed
end

for getting link we can use innerHTML