Last active
August 29, 2015 14:15
-
-
Save miry/9ce0285337e36601e3bd to your computer and use it in GitHub Desktop.
This file contains 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 'nokogiri' | |
require 'csv' | |
def parse_row(row) | |
result = {} | |
row.children.each do |el| | |
next if el.name == 'text' | |
result[el.name] = el.text.strip | |
end | |
result | |
end | |
def parse_card(scope) | |
return unless scope.css('TRANS_CARD').text == '4874-1229-0347-6528' | |
result = [] | |
scope.css('LIST_G_TRANS_DETAILS G_TRANS_DETAILS').each do |row| | |
result << parse_row(row).values | |
end | |
result | |
end | |
def xml2csv(file, csv) | |
f = File.open(file) | |
doc = Nokogiri::XML(f) | |
parse_card(doc.css('G_TRANS_CARD').last).each do |line| | |
csv << line | |
end | |
f.close | |
end | |
csv_string = CSV.generate do |csv| | |
csv << ["TRANS_DATE_XML", "POSTING_DATE_XML", "TRANS_AMOUNT", "TRANS_CURR", "FEE_AMOUNT", "ACC_AMOUNT", "ACCOUNT_AMOUNT", "TRANS_DETAILS"] | |
ARGV.each do|a| | |
xml2csv(a,csv) | |
end | |
end | |
puts csv_string | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment