Class: Badgerhash::XmlStream

Inherits:
Object
  • Object
show all
Defined in:
lib/badgerhash/xml_stream.rb

Overview

Converts XML as IO to a Badgerfish Hash using a stream parser

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, parser, io) ⇒ XmlStream

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize an XmlStream object.

Parameters:

  • handler (Badgerhash::Handlers::SaxHandler)

    the handler

  • parser (Class)

    a parser conforming to the required interface.

  • io (IO)

    object containing the XML to be parsed.



34
35
36
37
38
# File 'lib/badgerhash/xml_stream.rb', line 34

def initialize(handler, parser, io)
  @handler = handler
  @parser  = parser
  @io      = io
end

Class Method Details

.create(io) ⇒ Badgerhash::XmlStream

Create a properly initialized Badgerhash::XmlStream object

Examples:

io = StringIO.new("<alice>bob</alice>")
Badgerhash::XmlStream.create(io)

Parameters:

  • io (IO)

    an object containing the XML to be parsed

Returns:



12
13
14
# File 'lib/badgerhash/xml_stream.rb', line 12

def self.create(io)
  new(Handlers::SaxHandler.new, Badgerhash.sax_parser, io)
end

Instance Method Details

#to_badgerfishHash

The Badgerfish representation of the XML

Examples:

io = StringIO.new("<alice>bob</alice>")
Badgerhash::XmlStream.create(io).to_badgerfish => {"alice" => {"$" => "bob" }}

Returns:

  • (Hash)

    the Badgerfish hash



23
24
25
26
# File 'lib/badgerhash/xml_stream.rb', line 23

def to_badgerfish
  @parser.parse(@handler, @io)
  @handler.node.dup
end