使用 Nokogiri 和 Rails 3 从 API 解析 XML

发布于 2024-10-11 17:58:49 字数 1988 浏览 1 评论 0原文

您好,我正在尝试使用 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 saying
Missing 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2024-10-18 17:58:49

你真的在这里问两个问题..

是我的 xml ->解析->填充管道好吗?

是的,差不多。由于 .each 块中没有条件逻辑,因此这样做会更干净:

@news = doc.css('query').map do |n|
  #...
  News.new(:blah => blah, ...)
end

.. 但这是一个小问题。

编辑

您可以通过从解析的 xml 初始化哈希,然后将其传递给 Model.new 来节省一些输入,例如:

@news = doc.css('query').map do |n|
  h = {}
  h[:header] = n.css('header').text
  # ...
  News.new(h)
end

编辑 2

甚至更短..

@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

事实上,#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:

@news = doc.css('query').map do |n|
  #...
  News.new(:blah => blah, ...)
end

.. 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:

@news = doc.css('query').map do |n|
  h = {}
  h[:header] = n.css('header').text
  # ...
  News.new(h)
end

EDIT 2

Or even shorter..

@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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文