使用 Nokogiri 和 Rails 3 从 API 解析 XML
您好,我正在尝试使用 Nokogiri 从网站 API 解析 XML。我只是好奇我是否走在正确的轨道上。我有一个处理解析的控制器,然后我希望模型初始化必要的参数,然后将其显示为视图中的简单列表。我在控制器中思考类似的事情:
def index
doc = Nokogiri::XML(open("http://www.mysomething.com/partner/api/1_0/somerandomkeynumber4b0/channel/11number/material/list/").read)
@news = []
doc.css("news").each do |n|
header = n.css("header").text
source_name = n.css("source_name").text
summary = n.css("summary").text
url = i.css("url").text
created_at = i.css("created_at").text
type_of_media = i.css("type_of_media").text
@news << News.new(
:header => header,)
end
然后模型:
class News
include ActiveModel::Validations
validates_presence_of :url, :type_of_media
attr_accessor :header, :source_name, :summary, :url, :created_at, :type_of_media
def initialize(attributes = {})
@header = attributes[:header]
@source_name = attributes[:source_name]
@summary = attributes[:summary]
@url = attributes[:url]
@created_at = attributes[:created_at]
@type_of_media = attributes[:type_of_media]
end
你会这样做吗?!不确定我的想法是否正确。也许您对将 Nokogiri 与其他视图(例如 Google 地图等)结合起来的好方法有任何建议。现在我收到一条错误消息缺少带有 {:formats=>[:html], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml], :locale=>[:en 的模板新闻/索引, :en]} 在视图路径中
提前致谢!
@noodle:所以这个:
@news = doc.css('query').map do |n|
h = {}
%w(header source_name summary url created_at type_of_media).each do |key|
h[key.to_sym] = n.css(key).text
end
News.new(h)
end
等于:
@news = []
doc.css("news").each do |n|
header = n.css("header").text
source_name = n.css("source_name").text
summary = n.css("summary").text
url = i.css("url").text
created_at = i.css("created_at").text
type_of_media = i.css("type_of_media").text
@news << News.new(
:header => header,)
end
我理解正确吗?!关于模板我已经找到了问题所在。这是一个小拼写错误。干杯!
Hi Im trying to parse XML from a websites API with Nokogiri. Im just curious to see if Im on the right track. I have a controller wich handles the parsing and then I would like the model to initialize the necessary parameters and then display it as a simple list in the view. I was thinking something like this in the Controller:
def index
doc = Nokogiri::XML(open("http://www.mysomething.com/partner/api/1_0/somerandomkeynumber4b0/channel/11number/material/list/").read)
@news = []
doc.css("news").each do |n|
header = n.css("header").text
source_name = n.css("source_name").text
summary = n.css("summary").text
url = i.css("url").text
created_at = i.css("created_at").text
type_of_media = i.css("type_of_media").text
@news << News.new(
:header => header,)
end
and then the Model:
class News
include ActiveModel::Validations
validates_presence_of :url, :type_of_media
attr_accessor :header, :source_name, :summary, :url, :created_at, :type_of_media
def initialize(attributes = {})
@header = attributes[:header]
@source_name = attributes[:source_name]
@summary = attributes[:summary]
@url = attributes[:url]
@created_at = attributes[:created_at]
@type_of_media = attributes[:type_of_media]
end
Is this how you would do this?! Not sure Im thinking correct on this. Maybe you have any tips on a great way of incorporating Nokogiri with some other thing for the view like Google maps or something. Right now Im getting an error sayingMissing template news/index with {:formats=>[:html], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml], :locale=>[:en, :en]} in view paths
Thanks in advance!
@noodle: So this:
@news = doc.css('query').map do |n|
h = {}
%w(header source_name summary url created_at type_of_media).each do |key|
h[key.to_sym] = n.css(key).text
end
News.new(h)
end
Is equal to:
@news = []
doc.css("news").each do |n|
header = n.css("header").text
source_name = n.css("source_name").text
summary = n.css("summary").text
url = i.css("url").text
created_at = i.css("created_at").text
type_of_media = i.css("type_of_media").text
@news << News.new(
:header => header,)
end
Did I understand you correctly?! Regarding the template I have located the the problem. It was a minor misspelling. Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真的在这里问两个问题..
是我的 xml ->解析->填充管道好吗?
是的,差不多。由于 .each 块中没有条件逻辑,因此这样做会更干净:
.. 但这是一个小问题。
编辑
您可以通过从解析的 xml 初始化哈希,然后将其传递给
Model.new
来节省一些输入,例如:编辑 2
甚至更短..
事实上,#inject 可以让它更短,但我认为这会有点混乱。
为什么 Rails 找不到我的视图模板?
不知道,有吗?您没有提供足够的详细信息来回答该部分。
You're really asking two questions here..
Is my xml -> parse -> populate pipeline ok?
Yes, pretty much. As there's no conditional logic in your .each block it would be cleaner to do it like this:
.. but that's a minor point.
EDIT
You could save some typing by initializing a hash from the parsed xml and then passing that to
Model.new
, like:EDIT 2
Or even shorter..
In fact
#inject
could make that shorter still but I think that'd be a little obfuscated.Why can't rails find my view template?
Dunno, is there one? You've not given enough details to answer that part.