Created
February 10, 2021 12:06
-
-
Save ruhenheim/386f5da2e416b0a005590e92657515d4 to your computer and use it in GitHub Desktop.
convert xml to csv (pickuped fields only)
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
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