Class | REXML::Parsers::PullParser |
In: |
lib/rexml/parsers/pullparser.rb
|
Parent: | Object |
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.
# File lib/rexml/parsers/pullparser.rb, line 37 37: def initialize stream 38: @entities = {} 39: @listeners = nil 40: @parser = BaseParser.new( stream ) 41: @my_stack = [] 42: end
# File lib/rexml/parsers/pullparser.rb, line 44 44: def add_listener( listener ) 45: @listeners = [] unless @listeners 46: @listeners << listener 47: end
# File lib/rexml/parsers/pullparser.rb, line 49 49: def each 50: while has_next? 51: yield self.pull 52: end 53: end
# File lib/rexml/parsers/pullparser.rb, line 55 55: def peek depth=0 56: if @my_stack.length <= depth 57: (depth - @my_stack.length + 1).times { 58: e = PullEvent.new(@parser.pull) 59: @my_stack.push(e) 60: } 61: end 62: @my_stack[depth] 63: end
# File lib/rexml/parsers/pullparser.rb, line 65 65: def pull 66: return @my_stack.shift if @my_stack.length > 0 67: 68: event = @parser.pull 69: case event[0] 70: when :entitydecl 71: @entities[ event[1] ] = 72: event[2] unless event[2] =~ /PUBLIC|SYSTEM/ 73: when :text 74: unnormalized = @parser.unnormalize( event[1], @entities ) 75: event << unnormalized 76: end 77: PullEvent.new( event ) 78: end