So, firstly, here's an Atom feed snippet which I am trying to parse:
// http://somelink.开发者_开发知识库com/atom
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<title>Title Here</title>
<link href="http://somelink.com/link1&amp;ref=rss" rel="alternate" />
<link href="http://somelink.com/link2&amp;ref=rss" rel="tag:somelink.com/apply_url"/>
...
</entry>
I pull the Atom feed like so,
// In controller index method
@rss = SimpleRSS.parse open('http://somelink.com/atom')
Then I output the response in the view, which I am writing using Haml, as follows:
- @rss.entries.each do |item|
.title-div
= item.title
.title-link
= item.link //outputs the first link
I could run a second loop for the links but is there a way to get the second link without it? Like reading the "rel" attribute and outputting the correct link? How do I do this in Haml/Rails?
EDIT: The gem i am using: http://simple-rss.rubyforge.org/
I'm not familiar with that gem, but have you tried item.links
to see if each item provides a collection of links?
I have never used SimpleRSS but maybe you could give Nokogiri or Hpricot a try? You can than run an XPath query to only select the link with the right attribute. An example with Nokogiri:
atom_doc = Nokogiri::XML(open("http://www.example.com/atom.xml"))
atom_doc.xpath("/xmlns:feed/xmlns:entry/xmlns:link[@rel='tag:somelink.com/apply_url']")
Don't forget the namespaces if you are parsing an Atom feed.
精彩评论