class XTJ::Handlers::XMLHandler

Attributes

xml_hash[R]

Public Class Methods

new() click to toggle source
# File lib/xtj/handlers/xml_handler.rb, line 13
def initialize
  @xml_hash = {}
  @stack = []
end

Public Instance Methods

characters(text) click to toggle source
# File lib/xtj/handlers/xml_handler.rb, line 37
def characters(text)
  return if text.strip.empty?
  raise REXML::ParseException, 'Invalid XML' if @stack.empty?

  element = @stack.shift
  element.attributes[:@text] = text
  @stack.prepend(element)
end
end_element(_uri, _localname, _qname) click to toggle source
# File lib/xtj/handlers/xml_handler.rb, line 25
def end_element(_uri, _localname, _qname)
  if @stack.length > 1
    child = @stack.shift
    parent = @stack.shift
    parent.add_to_attributes(child.tag, child.attributes)
    @stack.prepend(parent)
  else
    element = @stack.shift
    @xml_hash[element.tag] = element.attributes
  end
end
start_element(_uri, _localname, qname, attributes) click to toggle source
# File lib/xtj/handlers/xml_handler.rb, line 18
def start_element(_uri, _localname, qname, attributes)
  prefixed_attributes = attributes
                        .transform_keys { |key| "_#{key}".to_sym }
  element = XMLTag.new(tag: qname.to_sym, attributes: prefixed_attributes)
  @stack.prepend(element)
end