简体   繁体   中英

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. The following is the entire rake task I have written. It is saved in lib/tasks/xml_parser.rake of my application.

  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. When I run the command, it returns "killed". My questions are:

(1) Is there anything obviously wrong with the above code?

(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?

(3) Assuming the code is complete and correct, why is it returning "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?

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. (Also, please check if you are correctly importing :organisation and :date_erected fields).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM