[英]rake task to parse xml from website into rails database
I am trying to import data from an online xml file into a rails database which I have already built. 我正在尝试将数据从在线xml文件导入到我已经构建的Rails数据库中。 The following is the entire rake task I have written.
以下是我编写的整个rake任务。 It is saved in lib/tasks/xml_parser.rake of my application.
它保存在我的应用程序的lib / tasks / xml_parser.rake中。
require 'open-uri'
doc = Nokogiri::XML(open("https://dl.dropboxusercontent.com/u/21695507/openplaques/gb_20151004.xml")) do |config|
config.options = Nokogiri::XML::ParseOptions::NOERROR
end
doc.css('plaque').each do |node|
children = node.children
Plaque.create(
:title => children.css('title').inner_text,
:subject => children.css('subjects').inner_text,
:colour => children.css('colour').inner_text,
:inscription => children.css('inscription raw').inner_text,
:latitude => children.css('geo')['latitude'],
:longitude => children.css('geo')['longitude'],
:address => children.css('address').inner_text,
:organisation => children.css('author').inner_text,
:date_erected => children.css('author').inner_text,
)
end
end
I am trying to run it from the command line with the command: $ rake plaques_import. 我正在尝试使用以下命令从命令行运行该命令:$ rake plaques_import。 When I run the command, it returns "killed".
当我运行命令时,它返回“ killed”。 My questions are:
我的问题是:
(1) Is there anything obviously wrong with the above code? (1)上面的代码明显有什么问题吗?
(2) Is there additional code I need to write either in the xml_parser.rake file or somewhere else, in order to create a rake task? (2)是否需要在xml_parser.rake文件或其他地方编写其他代码才能创建rake任务?
(3) Assuming the code is complete and correct, why is it returning "killed"? (3)假设代码完整正确,为什么返回“ killed”?
(4) Is there a good source which would show me step-by-step how to import xml from a website into a rails database? (4)有没有很好的资源可以逐步向我展示如何从网站将xml导入Rails数据库?
Thank you for your time. 感谢您的时间。
To begin with, you may remove the extra comma towards the end of the Create parentheses . 首先,您可以在创建括号的末尾删除多余的逗号。 If that doesn't work...
如果那不起作用...
Try this 尝试这个
require 'rake'
require 'open-uri'
namespace :xml_parser do
task :new_task => :environment do
doc = Nokogiri::XML(open("https://dl.dropboxusercontent.com/u/21695507/openplaques/gb_20151004.xml"))
doc.css('plaque').each do |node|
children = node.children
Plaque.create(
:title => children.css('title').inner_text,
:subject => children.css('subjects').inner_text,
:colour => children.css('colour').inner_text,
:inscription => children.css('inscription raw').inner_text,
:latitude => children.css('geo')['latitude'],
:longitude => children.css('geo')['longitude'],
:address => children.css('address').inner_text,
:organisation => children.css('author').inner_text,
:date_erected => children.css('author').inner_text
)
end
end
Then run rake xml_parser : new_task
That should work. 然后运行
rake xml_parser : new_task
应该可以。 (Also, please check if you are correctly importing :organisation
and :date_erected
fields). (另外,请检查您是否正确导入了
:organisation
和:date_erected
字段)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.