Created
October 19, 2011 16:33
-
-
Save mkempe/1298860 to your computer and use it in GitHub Desktop.
Using ActiveRecord / Nokogiri for xml → database convert action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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