Module: XML2CSV
- Defined in:
- lib/xml2csv.rb,
lib/xml2csv/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Class Method Summary collapse
- .get_content(node, param) ⇒ Object
- .parse_params(argv) ⇒ Object
- .parse_xml(xml_io, root_node, parameters) ⇒ Object
- .xml2csv ⇒ Object
Class Method Details
.get_content(node, param) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/xml2csv.rb', line 11 def get_content(node, param) key, values = param.to_a.first case key when "join" values.split(/,/).map{|value| node.xpath(value).map{|v| v.content.chomp}.join("-") }.join("_") when "pair" values.split(/,/).map{|value| node.xpath(value).map{|v| v.content.chomp} } when "with" wkey, wvalues = values.split(/:/, 2) wvalues.split(/,/).map{|value| node.xpath([wkey, value].join("/")).map{|v| v.content.chomp}.join("-") } when "digest" keys = values.split(/,/).map{|value| node.xpath(value).map{|v| v.content.chomp} } Digest::MD5.hexdigest keys.join when "with_order" ret = values.split(/,/).map{|value| if v = node.at_xpath(value) v.content.chomp else "" end } ret.empty? ? nil : ret.unshift(node.parent.children.index(node)) else values.split(/,/).map{|value| case key when "tag" value when "node", "xpath" then ret = node.xpath(value).map{|v| v.content.chomp} ret.empty? ? nil : ret when "attr" then ret = node[value] ret.nil? ? nil : ret.chomp end } end end |
.parse_params(argv) ⇒ Object
69 70 71 |
# File 'lib/xml2csv.rb', line 69 def parse_params(argv) Hash[*argv.split(/:/, 2)] end |
.parse_xml(xml_io, root_node, parameters) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/xml2csv.rb', line 61 def parse_xml(xml_io, root_node, parameters) Nokogiri::XML(xml_io).xpath("//#{root_node}").map do |node| parameters.flat_map{|param| get_content(node, param) } end end |
.xml2csv ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/xml2csv.rb', line 73 def xml2csv Signal.trap("PIPE", "EXIT") args = Slop.parse{ on :h, :help on :F=, '[field separater]' on :"no-header" }.to_hash if args.delete(:help) puts HELP exit end if col_sep = args.delete(:F) ["-F", col_sep].each{|s| ARGV.delete s} col_sep = eval %Q{"#{col_sep}"} end if no_header = args.delete(:"no-header") ARGV.delete "--no-header" end xml_io = $stdin.tty? ? File.read(ARGV.shift) : $stdin.read root_node = ARGV.shift parameters = ARGV.map{|argv| parse_params(argv)} header = parameters.map{|h| h.each_pair.map{|k,v| v.split(/,/).map{|_v| File.basename _v} } }.flatten = {force_quotes: true, headers: header, write_headers: true} if col_sep .merge! col_sep: col_sep .delete :force_quotes end if no_header [:headers, :write_headers].each{|key| .delete key } end print CSV.generate("", ){|csv| parse_xml(xml_io, root_node, parameters).each{|row| row = row.flatten csv << row unless row.empty? } } end |