Module: Xml2Hash

Defined in:
lib/xml2hash.rb,
lib/xml2hash/version.rb,
lib/xml2hash/typical_handler.rb

Defined Under Namespace

Modules: TypicalHandler Classes: WithNokogiri, WithOx

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.parse(stream, parser = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xml2hash.rb', line 18

def self.parse(stream, parser=nil)
  stream = StringIO.new(stream) if stream.is_a?(String)

  if parser.nil?
    if defined?(WithOx)
      parser = :ox
    elsif defined?(WithNokogiri)
      parser = :nokogiri
    end
  end

  if parser == :ox && defined?(WithOx)
    handler = WithOx.new
    Ox.sax_parse(handler, stream)
    handler.pointer

  elsif parser == :nokogiri && defined?(WithNokogiri)
    handler = WithNokogiri.new
    Nokogiri::XML::SAX::Parser.new(handler).parse(stream)
    handler.pointer

  else
    raise 'No SAX XML parser available'

  end
end