1
我試圖使用libxml的SAX解析器(如illustrated here),但我遇到未定義的方法錯誤。使用libxml的sax解析器時未定義的方法錯誤
我的代碼是
$domain_topics = Hash.new { |h,d| h[d] = [] }
parser = LibXML::XML::SaxParser.io(
File.open("content.rdf.u8", "r:UTF-8")
)
class Callbacks
include LibXML::XML::SaxParser::Callbacks
def initialize
@state = :top
@topics = nil
end
def on_start_element(element, attributes)
case @state
when :top
return unless element == 'ExternalPage'
@state = :ExternalPage
domain = attributes['about'].sub(%r!^\w+://([^"/]*)(?:/[^"]*)?$!, '\1')
@topics = $domain_topics[domain]
when :ExternalPage
return unless element == 'topic'
@state = :topic
end
end
def on_characters(characters)
if @state == :topic and @topics
@topics << characters
end
end
def on_end_element(element)
case @state
when :ExternalPage
@state = :top
@topics = nil
when :topic
@state = :ExternalPage
end
end
end
parser.callbacks = Callbacks
parser.parse
當我運行它:
% ./my_awesome_code.rb
./my_awesome_code.rb:1337:in `parse': undefined method `on_start_document' for Callbacks:Class (NoMethodError)
我在做什麼錯在這裏? include LibXML::XML::SaxParser::Callbacks
不應該給出默認定義 on_start_document
?
IRB似乎也印證了我的直覺:
1.9.3p194 :009 > Callbacks.instance_methods.include? :on_start_document
=> true