Skip to content

Instantly share code, notes, and snippets.

@ruhenheim
Created February 10, 2021 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruhenheim/386f5da2e416b0a005590e92657515d4 to your computer and use it in GitHub Desktop.
Save ruhenheim/386f5da2e416b0a005590e92657515d4 to your computer and use it in GitHub Desktop.
convert xml to csv (pickuped fields only)
require 'csv'
require 'nokogiri'
filename = "1day_checkup"
file_names = Dir.open('.',&:to_a).reject{|f| !f.include?(".xml")}
CSV.open("#{filename}.csv", "wb") do |csv|
csv << %w(保険者番号 被保険者証等記号 被保険者証等番号 カナ氏名)
file_names.each do |filename|
# 拡張子とファイル名を分離
# name, ext = /\A(.+?)((?:\.[^.]+)?)\z/.match(file_name, &:captures)
doc = File.open("#{filename}") { |f| Nokogiri::XML(f) }
entries = doc.css('recordTarget')
rows = entries.css('id').map{|node| node.get_attribute('extension')}
rows << entries.at_css('patient name').text
csv << rows
end
end
# puts "Converted #{csv.size} elements."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment