lib/fluent/plugin/parser_xml.rb in fluent-plugin-xml-parser-0.0.1 vs lib/fluent/plugin/parser_xml.rb in fluent-plugin-xml-parser-0.0.2

- old
+ new

@@ -6,12 +6,13 @@ Plugin.register_parser("xml", self) # How to specify the target attributes and values # The followings are an example description for Libelium SmartCity sensor data. # - # attr_xpaths '[[null, "@timestamp"], ["cap:alert/cap:info/cap:parameter/cap:valueName", "text"], [null, "location"]]' - # value_xpaths '[["cap:alert/cap:info/cap:onset", "text"], ["cap:alert/cap:info/cap:parameter/cap:value", "text"], [null, "Kyoto"]]' + # time_xpath '["cap:alert/cap:info/cap:onset", "text"]' + # attr_xpaths '[[null, "description"], ["cap:alert/cap:info/cap:parameter/cap:valueName", "text"]]' + # value_xpaths '[["cap:alert/cap:info/cap:description", "text"], ["cap:alert/cap:info/cap:parameter/cap:value", "text"]]' # # attr_xpaths indicates attribute name of the target value. Each array with two strings # means xpath of the attribute name and the attribute of the XML element (name, text etc). # XPath can be omitted as 'null' and specify your own attribute name as the second # parameter. @@ -24,17 +25,19 @@ # # require 'rexml/document' # doc = REXML::Document.new(open("test.xml")) # doc.elements['cap:alert/cap:info'].children # + config_param :time_xpath, :string, :default => '[]' config_param :attr_xpaths, :string, :default => '[]' config_param :value_xpaths, :string, :default => '[]' config_param :time_format, :string, :default => nil # time_format is configurable # This method is called after config_params have read configuration parameters def configure(conf) super + @time_xpath = json_parse(conf['time_xpath']) @attr_xpaths = json_parse(conf['attr_xpaths']) @value_xpaths = json_parse(conf['value_xpaths']) @time_format = conf['time_format'] # TimeParser class is already given. It takes a single argument as the time format # to parse the time string with. @@ -46,10 +49,12 @@ # it is a single syslog message. def parse(text) begin doc = REXML::Document.new(text) $log.debug doc + # parse time field + @time = @time_parser.parse(doc.elements[@time_xpath[0]].method(@time_xpath[1]).call) record = {} attrs = @attr_xpaths.map do |attr_xpath| if attr_xpath[0].nil? # when null is specified attr_xpath[1] # second parameter is used as the attribute name else # otherwise, the target attribute name is extracted from XML @@ -62,15 +67,10 @@ else # otherwise, the target value is extracted from XML doc.elements[value_xpath[0]].method(value_xpath[1]).call end end attrs.size.times do |i| - if i == 0 # time value - @time = @time_parser.parse(values[i]) - record[attrs[i]] = @time - else - record[attrs[i]] = values[i] - end + record[attrs[i]] = values[i] end yield @time, record rescue REXML::ParseException => e $log.warn "Parse error", :error => e.to_s $log.debug_backtrace(e.backtrace)