尝试从深层嵌套的内容中获取信息。想要获取rails中每个条目的TopicName和TopicID。
我得到的只是@content="\n \n \n
<entry>
<id>...</id>
<title type="text">Ground Water Awareness Week</title>
<summary type="text"/>
<updated>...</updated>
<link rel="alternate" href="..."/>
<link href="..."/>
<content type="application/xml">
<CatalogItems>
<CatalogSource Acronym="..." OrganizationName="..."/>
<CatalogItem Id="25743" CatalogUrl="..." AddedDateTime="..." ModifiedDateTime="...">
<ContentItem TargetUrl="..." ContentType="text/xml" CreatedDateTime="..." ModifiedDateTime="..." PersistentUrl="..." Title="..." GUID="...">
<content:SelectionSpec ClassList="" ElementList=""/>
<content:Language Value="eng" Scheme="ISO 639-2"/>
<content:Source Acronym="..." OrganizationName="..."/>
<content:Topics Scheme="...">
<content:Topic TopicId="27106" TopicName="drinking water safety"/>
<content:Topic TopicId="27105" TopicName="Ground water"/>
<content:Topic TopicId="27107" TopicName="clean drinking water"/>
</content:Topics>
</ContentItem>
</CatalogItem>
</CatalogItems>
</content>
</entry>
feed_entry.rb
class FeedEntry < ActiveRecord::Base
require 'feedzirra'
def self.update_from_feed(feed_url)
feed = Feedzirra::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries)
end
def self.add_entries(entries)
entries.each do |entry|
unless exists? :guid => entry.id
create!(:name => entry.title, :summary => entry.summary, :url => entry.url, :published_at => entry.published, :guid => entry.id)
# After entry is created
# I then want to check if topic exists or not. if not then create.
# something like below
CatalogItems.CatalogSource.content:Topics.each do |topic|
Catagory.find_or_create_by_name(:name => TopicName)
end
end
end
end
end