Class: ToHashParser

Inherits:
Object show all
Defined in:
lib/extlib/hash.rb

Class Method Summary collapse

Class Method Details

.from_xml(xml) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/extlib/hash.rb', line 426

def self.from_xml(xml)
  stack = []
  parser = REXML::Parsers::BaseParser.new(xml)

  while true
    event = parser.pull
    case event[0]
    when :end_document
      break
    when :end_doctype, :start_doctype
      # do nothing
    when :start_element
      stack.push REXMLUtilityNode.new(event[1], event[2])
    when :end_element
      if stack.size > 1
        temp = stack.pop
        stack.last.add_node(temp)
      end
    when :text, :cdata
      stack.last.add_node(event[1]) unless event[1].strip.length == 0
    end
  end
  stack.pop.to_hash
end