Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created August 21, 2010 05:47
Show Gist options
  • Save seungjin/541843 to your computer and use it in GitHub Desktop.
Save seungjin/541843 to your computer and use it in GitHub Desktop.
export mysql's table into xml
require 'Database_connection'
require 'rubygems' #ruby 1.9 does not need it
require 'builder'
require 'iconv'
$KCODE = "UTF-8"
$dbcon = Database_connection.new().seungjin()
$builder = Builder::XmlMarkup.new(:target=>$resultxml="", :indent=>2, :encoding=>'UTF-8')
$builder.instruct!(:xml, :version => "1.0", :encoding => "UTF-8")
def table_exist?(table_name)
r = false
for t in $dbcon.list_tables.each do
if t == table_name
r = true
end
end
return r
end
def export_table(table_name)
if table_exist?(table_name) == true
res = $dbcon.query("select * from " + table_name)
res.each_hash(with_table=false) do |row|
$builder.row { |b|
for i in res.fetch_fields do
eval("b.#{i.name}(row['#{i.name}'])")
end
}
end
end
end
ARGV.each do |table_name|
export_table(table_name)
puts $resultxml
end
@seungjin
Copy link
Author

have issues with utf-8.
English utf-8 works just fine.
Korean utf-8 got broken ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment