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,
}.freeze

Class Method Summary collapse

Class Method Details

.parse(xml_or_io, &block) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/leptris/xml/pull.rb', line 106

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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/leptris/xml/pull.rb', line 119

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