Skip to content

Instantly share code, notes, and snippets.

@mkempe
Created October 19, 2011 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mkempe/1298860 to your computer and use it in GitHub Desktop.
Save mkempe/1298860 to your computer and use it in GitHub Desktop.
Using ActiveRecord / Nokogiri for xml → database convert action
# encoding: utf-8
require 'active_record'
require 'mysql2'
require 'nokogiri'
require 'yaml'
ROOT = File.join(File.dirname(__FILE__), '..')
# ActiveRecord::Base.logger = Logger.new('log/debug.log')
ActiveRecord::Base.configurations = YAML::load(IO.read('config/database.yml'))
ActiveRecord::Base.establish_connection('development')
class Country < ActiveRecord::Base
set_table_name 'static_countries'
set_primary_key 'uid'
end
xml = Nokogiri::XML(IO.read('data/ammap_data.xml'))
xml.css('map areas area').each do |area|
oid = area.attribute('oid')
if oid
country = Country.find_by_cn_iso_2(oid.to_str)
if country
country.zoom = area.attribute('zoom').to_str
country.zoom_x = area.attribute('zoom_x').to_str
country.zoom_y = area.attribute('zoom_y').to_str
country.save!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment