Module: Xml2Hash::TypicalHandler

Included in:
WithNokogiri, WithOx
Defined in:
lib/xml2hash/typical_handler.rb

Instance Method Summary collapse

Instance Method Details

#characters(value) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/xml2hash/typical_handler.rb', line 45

def characters(value)
  if @stack.last[@name].is_a?(Array)
    @stack.last[@name].pop
    @stack.last[@name] << value
  else
    @stack.last[@name] = value
  end
end

#end_element(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xml2hash/typical_handler.rb', line 29

def end_element(name)
  @pointer = @stack.pop

  # Ox doesn't send text event for blank tag so we have to
  # clean up that when tag closes
  if @pointer[@name] == {}
    @pointer[@name] = nil
  end

  # Ox doesn't send text event for blank tag so we have to
  # clean up that when tag closes
  if @pointer[@name].is_a?(Array) && @pointer[@name].last == {}
    @pointer[@name][@pointer[@name].length - 1] = nil
  end
end

#initializeObject



3
4
5
6
# File 'lib/xml2hash/typical_handler.rb', line 3

def initialize
  @pointer = {}
  @stack   = []
end

#pointerObject



8
9
10
# File 'lib/xml2hash/typical_handler.rb', line 8

def pointer
  @pointer
end

#start_element(name, attrs = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xml2hash/typical_handler.rb', line 12

def start_element(name, attrs=[])
  @stack.push(@pointer)

  @name    = name.to_s
  @pointer = {}

  if @stack.last.include?(@name)
    if @stack.last[@name].is_a?(Array)
      @stack.last[@name] << @pointer
    else
      @stack.last[@name] = [@stack.last[@name], @pointer]
    end
  else
    @stack.last[@name] = @pointer
  end
end