如何在 Rails 中导入 XML 数据 5

How to Import XML Data in Rails 5

我有 xml 数据,我正在尝试导入我的数据库。我正在尝试使用 nokogiri gem。我写了一个名为 xml_data.rb 的文件。当我在命令行上 运行 >ruby xml_data.rb 时,我收到以下错误:

xml_data.rb:8:in block in <main>': uninitialized constant Job (NameError) from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:187:inblock in each' from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in upto' from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:ineach' from xml_data.rb:7:in `'

我不确定我是否了解如何正确执行此操作。下面是 xml_data.rb 文件:

require 'nokogiri'

OLD_DATA = 'data/old_data.xml'

doc = File.open(OLD_DATA) {|f| Nokogiri::XML(f)}

doc.css('request').each do |node|
    Job.create(
        :last_name => node['name'],
        :telephone => node['phone'],
        :street_address => node['address'],
        :city => node['city'],
        :state => node['state'],
        :zip => node['zip'],
        :email => node['email'],
        :au_chog => node['chogAu'],
        :person_type => node['affil'],
        :research_use => node['use'],
        :subject => node['subject'],
        :notes => node['note'],
        :start_date => node['startDate'],
        :end_date => node['addDate'],
        :complete => true,
        :time_spend => node['hours']
    )
end

Ruby: v2.3.3 Rails: v5.0.1

更新

我将这段代码重写为一个 rake 任务。但是,我没有这样做。当我 运行 任务时,我得到几个与 XML 语法有关的语法错误,这是正确的。以下是更新后的代码:

require 'nokogiri'

namespace :db do
  namespace :seed do

    desc 'Add old data to database'
    task :import_data => :environment do


        OLD_DATA = "#{Rails.root}/lib/assets/data/old_data.xml"

        doc = Nokogiri::XML(File.open(OLD_DATA)) do |config|
            config.options = Nokogiri::XML::ParseOptions::STRICT
        end

        doc.css("request").each do |node|
            Job.create(
                :last_name => node['name'],
                :telephone => node['phone'],
                :street_address => node['address'],
                :city => node['city'],
                :state => node['state'],
                :zip => node['zip'],
                :email => node['email'],
                :au_chog => node["chogAu"],
                :person_type => node['affil'],
                :research_use => node['use'],
                :subject => node['subject'],
                :notes => node['note'],
                :start_date => node['startDate'],
                :end_date => node['addDate'],
                :complete => true,
                :time_spend => node['hours']
            )
        end
    end
  end
end

作业未定义,因为您实际上 运行 它不在 Rails 环境中。

添加

require File.expand_path(File.dirname(__FILE__) + 'config/environment')

如果您不在 rails 范围内,您可以试试这样的方法。

require 'nokogiri'
require 'active_record'

ActiveRecord::Base.establish_connection(
  adapter:  'mysql2', # or 'postgresql' or 'sqlite3' or 'oracle_enhanced'
  host:     'localhost',
  database: 'your_database',
  username: 'your_username',
  password: 'your_password'
)

class Job < ActiveRecord::Base
end

class ExampleData
  attr_reader :file

  def initialize(file)
    @file = file
  end

  def xml
    Nokogiri::XML(open(file))
  end

  def create_jobs
    xml.css('request').each do |node|
      Job.create(
        :last_name => node['name'],
        :telephone => node['phone'],
        :street_address => node['address'],
        :city => node['city'],
        :state => node['state'],
        :zip => node['zip'],
        :email => node['email'],
        :au_chog => node['chogAu'],
        :person_type => node['affil'],
        :research_use => node['use'],
        :subject => node['subject'],
        :notes => node['note'],
        :start_date => node['startDate'],
        :end_date => node['addDate'],
        :complete => true,
        :time_spend => node['hours']
    )
    end
  end


  file = File.dirname(__FILE__) + "/data/old_data.xml"
  example_data = ExampleData.new(file)
  example_data.create_jobs
end

重写为 rake 任务。

require 'nokogiri'

namespace :db do
    namespace :seed do

        desc 'Add old data to database'
        task :import_data => :environment do

            OLD_DATA = "#{Rails.root}/lib/assets/data/old_data.xml"

            doc = Nokogiri::XML(File.open(OLD_DATA)) do |config|
                config.options = Nokogiri::XML::ParseOptions::STRICT
            end

            doc.css("request").each do |node|
                Job.create(
                    :last_name => node['name'],
                    :telephone => node['phone'],
                    :street_address => node['address'],
                    :city => node['city'],
                    :state => node['state'],
                    :zip => node['zip'],
                    :email => node['email'],
                    :au_chog => node["chogAu"],
                    :person_type => node['affil'],
                    :research_use => node['use'],
                    :subject => node['subject'],
                    :notes => node['note'],
                    :start_date => node['startDate'],
                    :end_date => node['addDate'],
                    :complete => true,
                    :time_spend => node['hours']
                )
            end
        end
    end
end