class Nokogiri::XML::SAX::Parser
see Nokogiri::XML::SAX::Document for the available events.
For more information about SAX parsers, see Nokogiri::XML::SAX. Also<br><br>parser.parse(File.open(ARGV))
# Send some XML to the parser
parser = Nokogiri::XML::SAX::Parser.new(MyDoc.new)
# Create our parser
end
end
puts “ending: #{name}”
def end_element name
end
puts “starting: #{name}”
def start_element name, attrs = []
class MyDoc < Nokogiri::XML::SAX::Document
# the events we care about:
# Create a subclass of Nokogiri::XML::SAX::Document and implement
Here is an example of using this parser:
the Nokogiri::XML::SAX::Document.
an optional encoding, then given an XML input, sends messages to
deems necessary. The parser takes a Nokogiri::XML::SAX::Document,
This parser is a SAX style parser that reads it’s input as it
##
def check_encoding(encoding)
def check_encoding(encoding) encoding.upcase! raise ArgumentError.new("'#{encoding}' is not a valid encoding") unless ENCODINGS[encoding] end
def initialize doc = Nokogiri::XML::SAX::Document.new, encoding = 'UTF-8'
def initialize doc = Nokogiri::XML::SAX::Document.new, encoding = 'UTF-8' check_encoding(encoding) @encoding = encoding @document = doc @warned = false end
def parse thing, &block
Parse given +thing+ which may be a string containing xml, or an
##
def parse thing, &block if thing.respond_to?(:read) && thing.respond_to?(:close) parse_io(thing, &block) else parse_memory(thing, &block) end end
def parse_file filename
##
def parse_file filename raise ArgumentError unless filename raise Errno::ENOENT unless File.exist?(filename) raise Errno::EISDIR if File.directory?(filename) ctx = ParserContext.file filename yield ctx if block_given? ctx.parse_with self end
def parse_io io, encoding = 'ASCII'
##
def parse_io io, encoding = 'ASCII' check_encoding(encoding) @encoding = encoding ctx = ParserContext.io(io, ENCODINGS[encoding]) yield ctx if block_given? ctx.parse_with self end
def parse_memory data
def parse_memory data ctx = ParserContext.memory data yield ctx if block_given? ctx.parse_with self end