Class: REXML::Parsers::PullParser (Ruby 2.4.2)
Description: Class : REXML::Parsers::PullParser - Ruby 2.4.2

Support for the Ruby 2.4 series has ended. See here for reference.

### Home Classes Methods

### In Files

- rexml/parsers/pullparser.rb

### Parent

Object

### Methods

- ::new
- #add_listener
- #each
- #peek
- #pull
- #unshift

### Included Modules

- REXML::XMLTokens

### Class/Module Index

Quicksearch

- Array
- Float
- Integer
- Object
- REXML
- REXML::AttlistDecl
- REXML::Attribute
- REXML::Attributes
- REXML::CData
- REXML::Child
- REXML::Comment
- REXML::DTD
- REXML::DTD::AttlistDecl
- REXML::DTD::ElementDecl
- REXML::DTD::EntityDecl
- REXML::DTD::NotationDecl
- REXML::DTD::Parser
- REXML::Declaration
- REXML::DocType
- REXML::Document
- REXML::Element
- REXML::ElementDecl
- REXML::Elements
- REXML::Encoding
- REXML::Entity
- REXML::EntityConst
- REXML::ExternalEntity
- REXML::Formatters
- REXML::Formatters::Default
- REXML::Formatters::Pretty
- REXML::Formatters::Transitive
- REXML::Functions
- REXML::IOSource
- REXML::Instruction
- REXML::Light
- REXML::Light::Node
- REXML::Namespace
- REXML::Node
- REXML::NotationDecl
- REXML::Output
- REXML::Parent
- REXML::ParseException
- REXML::Parsers
- REXML::Parsers::BaseParser
- REXML::Parsers::LightParser
- REXML::Parsers::PullEvent
- REXML::Parsers::PullParser
- REXML::Parsers::SAX2Parser
- REXML::Parsers::StreamParser
- REXML::Parsers::TreeParser
- REXML::Parsers::UltraLightParser
- REXML::Parsers::XPathParser
- REXML::QuickPath
- REXML::SAX2Listener
- REXML::Security
- REXML::Source
- REXML::SourceFactory
- REXML::StreamListener
- REXML::SyncEnumerator
- REXML::Text
- REXML::UndefinedNamespaceException
- REXML::Validation
- REXML::Validation::Choice
- REXML::Validation::Event
- REXML::Validation::Interleave
- REXML::Validation::OneOrMore
- REXML::Validation::Optional
- REXML::Validation::Ref
- REXML::Validation::RelaxNG
- REXML::Validation::Sequence
- REXML::Validation::State
- REXML::Validation::ValidationException
- REXML::Validation::Validator
- REXML::Validation::ZeroOrMore
- REXML::XMLDecl
- REXML::XMLTokens
- REXML::XPath
- REXML::XPathParser
- Symbol

No matching classes.

# REXML::Parsers::PullParser

# Using the Pull Parser¶ ↑

This API is experimental, and subject to change.

    parser = PullParser.new( "<a>text<b att='val'/>txet</a>" )
    while parser.has_next?
      res = parser.next
      puts res[1]['att'] if res.start_tag? and res[0] == 'b'
    end

See the `PullEvent` class for information on the content of the results. The data is identical to the arguments passed for the various events to the `StreamListener` API.

Notice that:

    parser = PullParser.new( "<a>BAD DOCUMENT" )
    while parser.has_next?
      res = parser.next
      raise res[1] if res.error?
    end

Nat Price gave me some good ideas for the API.

### Public Class Methods

new(stream) click to toggle source

                   # File rexml/parsers/pullparser.rb, line 38
    def initialize stream
      @entities = {}
      @listeners = nil
      @parser = BaseParser.new( stream )
      @my_stack = []
    end

### Public Instance Methods

add_listener( listener ) click to toggle source

                   # File rexml/parsers/pullparser.rb, line 45
    def add_listener( listener )
      @listeners = [] unless @listeners
      @listeners << listener
    end

each() click to toggle source

                   # File rexml/parsers/pullparser.rb, line 50
    def each
      while has_next?
        yield self.pull
      end
    end

peek(depth=0) click to toggle source

                   # File rexml/parsers/pullparser.rb, line 56
    def peek depth=0
      if @my_stack.length <= depth
        (depth - @my_stack.length + 1).times {
          e = PullEvent.new(@parser.pull)
          @my_stack.push(e)
        }
      end
      @my_stack[depth]
    end

pull() click to toggle source

                   # File rexml/parsers/pullparser.rb, line 66
    def pull
      return @my_stack.shift if @my_stack.length > 0

      event = @parser.pull
      case event[0]
      when :entitydecl
        @entities[ event[1] ] =
          event[2] unless event[2] =~ /PUBLIC|SYSTEM/
      when :text
        unnormalized = @parser.unnormalize( event[1], @entities )
        event << unnormalized
      end
      PullEvent.new( event )
    end

unshift(token) click to toggle source

                   # File rexml/parsers/pullparser.rb, line 81
    def unshift token
      @my_stack.unshift token
    end