Module: Nori::Parser::REXML

Defined in:
lib/nori/parser/rexml.rb

Overview

Nori::Parser::REXML

REXML pull parser.

Class Method Summary collapse

Class Method Details

.parse(xml, options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nori/parser/rexml.rb', line 11

def self.parse(xml, options)
  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 Nori::XMLUtilityNode.new(options, 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 || stack.empty?
    end
  end
  stack.length > 0 ? stack.pop.to_hash : {}
end