Class: ETL::Parser::XmlParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/etl/parser/xml_parser.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary

Attributes inherited from Parser

#options, #source

Instance Method Summary collapse

Methods inherited from Parser

class_for_name

Constructor Details

#initialize(source, options = {}) ⇒ XmlParser

Initialize the parser

  • source: The Source object

  • options: Parser options Hash



9
10
11
12
# File 'lib/etl/parser/xml_parser.rb', line 9

def initialize(source, options={})
  super
  configure
end

Instance Method Details

#eachObject

Returns each row



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/etl/parser/xml_parser.rb', line 15

def each
  Dir.glob(file).each do |file|
    doc = nil
    t = Benchmark.realtime do
      doc = REXML::Document.new(File.new(file))
    end
    Engine.logger.info "XML #{file} parsed in #{t}s"
    doc.elements.each(@collection_xpath) do |element|
      row = {}
      fields.each do |f|
        value = element.text(f.xpath)
        row[f.name] = value
      end
      yield row
    end
  end
end

#fieldsObject

Get an array of defined fields



34
35
36
# File 'lib/etl/parser/xml_parser.rb', line 34

def fields
  @fields ||= []
end