Class: CSVDiff::XMLSource
Overview
Convert XML content to CSV format using XPath selectors to identify the rows and field values in an XML document
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Attributes inherited from Source
#case_sensitive, #child_field_indexes, #child_fields, #data, #dup_count, #field_names, #index, #key_field_indexes, #key_fields, #line_count, #lines, #parent_field_indexes, #parent_fields, #path, #skip_count, #trim_whitespace, #warnings
Instance Method Summary collapse
-
#initialize(path, options = {}) ⇒ XMLSource
constructor
Create a new XMLSource, identified by
path
. -
#process(source, rec_xpath, field_maps, context = nil) ⇒ Object
Process a
source
, converting the XML into a table of data, usingrec_xpath
to identify the nodes that correspond each record that should appear in the output, andfield_maps
to populate each field in each row.
Methods inherited from Source
#[], #index_source, #path?, #save_csv, #to_hash
Constructor Details
#initialize(path, options = {}) ⇒ XMLSource
Create a new XMLSource, identified by path
. Normally this is a path to the XML document, but any value is fine, as it is just a label to identify this data set.
43 44 45 46 47 48 |
# File 'lib/csv-diff/xml_source.rb', line 43 def initialize(path, = {}) super() @path = path @context = [:context] @data = [] end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
11 12 13 |
# File 'lib/csv-diff/xml_source.rb', line 11 def context @context end |
Instance Method Details
#process(source, rec_xpath, field_maps, context = nil) ⇒ Object
Process a source
, converting the XML into a table of data, using rec_xpath
to identify the nodes that correspond each record that should appear in the output, and field_maps
to populate each field in each row.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/csv-diff/xml_source.rb', line 77 def process(source, rec_xpath, field_maps, context = nil) @field_names = field_maps.keys unless @field_names case source when Nokogiri::XML::Document add_data(source, rec_xpath, field_maps, context || @context) when /<\?xml/ doc = Nokogiri::XML(source) add_data(doc, rec_xpath, field_maps, context || @context) when Array source.each{ |f| process_file(f, rec_xpath, field_maps) } when String process_file(source, rec_xpath, field_maps) else raise ArgumentError, "Unhandled source type #{source.class.name}" end @data end |