class REXML::Parsers::PullParser

使用 Pull Parser

此 API 为实验性,可能会发生更改。

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

有关结果内容的信息,请参阅 PullEvent 类。数据与传递给 StreamListener API 的各种事件的参数相同。

请注意

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

Nat Price 为这个 API 提供了一些很好的想法。

公共类方法

new(stream) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 38
def initialize stream
  @entities = {}
  @listeners = nil
  @parser = BaseParser.new( stream )
  @my_stack = []
end

公共实例方法

add_listener( listener ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 45
def add_listener( listener )
  @listeners = [] unless @listeners
  @listeners << listener
end
each() { |pull| ... } 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 62
def each
  while has_next?
    yield self.pull
  end
end
entity_expansion_count() 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 50
def entity_expansion_count
  @parser.entity_expansion_count
end
entity_expansion_limit=( limit ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 54
def entity_expansion_limit=( limit )
  @parser.entity_expansion_limit = limit
end
entity_expansion_text_limit=( limit ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 58
def entity_expansion_text_limit=( limit )
  @parser.entity_expansion_text_limit = limit
end
peek(depth=0) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 68
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() 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 78
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
reset() 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 97
def reset
  @parser.reset
end
unshift(token) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/parsers/pullparser.rb, line 93
def unshift token
  @my_stack.unshift token
end