Module: Leptris::XML::Pull

Defined in:
lib/leptris/xml/pull.rb

Overview

Pull parsing (libleptris v1.6.0): a StAX-style cursor over a document. Each #next advances one event; attributes are readable only during the start_element event just returned.

Leptris::XML::Pull.parse(xml).each do |event|
  case event.type
  when :start_element then handle(event.name, event.attrs)
  when :text then handle(event.text)
  end
end

Defined Under Namespace

Classes: Event, Parser

Constant Summary collapse

TYPES =
{
  Leptris::XML::FFI::PULL_START_ELEMENT => :start_element,
  Leptris::XML::FFI::PULL_END_ELEMENT => :end_element,
  Leptris::XML::FFI::PULL_TEXT => :text,
  Leptris::XML::FFI::PULL_COMMENT => :comment,
  Leptris::XML::FFI::PULL_CDATA => :cdata,
  Leptris::XML::FFI::PULL_PI => :pi,
  Leptris::XML::FFI::PULL_END_DOCUMENT => :end_document,
  Leptris::XML::FFI::PULL_ERROR => :error,
  # Prefix events ride the stream ("" prefix = the default
  # namespace): the engine has always delivered them; the map
  # lacked the kinds, silently yielding nil-typed Events — which
  # the staging guard then misread as corruption after the real
  # engine fix landed (leptris/leptris#648's residual).
  Leptris::XML::FFI::PULL_START_PREFIX => :start_prefix,
  Leptris::XML::FFI::PULL_END_PREFIX => :end_prefix,
}.freeze

Class Method Summary collapse

Class Method Details

.parse(xml_or_io, &block) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/leptris/xml/pull.rb', line 187

def self.parse(xml_or_io, &block)
  parser = Parser.parse(xml_or_io)
  if block
    begin
      parser.each(&block)
    ensure
      parser.free
    end
  else
    parser
  end
end

.parse_file(path, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/leptris/xml/pull.rb', line 200

def self.parse_file(path, &block)
  parser = Parser.parse_file(path)
  if block
    begin
      parser.each(&block)
    ensure
      parser.free
    end
  else
    parser
  end
end