Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • rexml/parsers/pullparser.rb

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 37
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 44
def add_listener( listener )
  @listeners = [] unless @listeners
  @listeners << listener
end
            
each() click to toggle source
 
               # File rexml/parsers/pullparser.rb, line 49
def each
  while has_next?
    yield self.pull
  end
end
            
peek(depth=0) click to toggle source
 
               # File rexml/parsers/pullparser.rb, line 55
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 65
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 80
def unshift token
  @my_stack.unshift token
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus