Class: Crack::XML

Inherits:
Object show all
Defined in:
lib/crack/xml.rb

Class Method Summary collapse

Class Method Details

.parse(xml) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/crack/xml.rb', line 192

def self.parse(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 || stack.empty?
    end
  end
  stack.length > 0 ? stack.pop.to_hash : {}
end