class Nokogiri::XML::SAX::Parser
For HTML documents, use the subclass Nokogiri::HTML4::SAX::Parser.
Also see Nokogiri::XML::SAX::Document for the available events.
For more information about SAX parsers, see Nokogiri::XML::SAX.
end
parser.parse(f)
File.open(path_to_xml) do |f|
# Hand an IO object to the parser, which will read the XML from the IO.
parser = Nokogiri::XML::SAX::Parser.new(MyHandler.new)
end
end
puts “ending: #{name}”
def end_element name
end
puts “starting: #{name}”
def start_element name, attrs = []
class MyHandler < 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:
messages to the Nokogiri::XML::SAX::Document.
takes a Nokogiri::XML::SAX::Document, an optional encoding, then given an XML input, sends
This parser is a SAX style parser that reads its input as it deems necessary. The parser
##
def initialize(doc = Nokogiri::XML::SAX::Document.new, encoding = nil)
parsing the input. (default +nil+ for auto-detection)
- +encoding+ (optional Encoding, String, nil) An Encoding or encoding name to use when
through the #document attribute.
events. Will create a new Nokogiri::XML::SAX::Document if not given, which is accessible
- +handler+ (optional Nokogiri::XML::SAX::Document) The document that will receive
[Parameters]
Create a new Parser.
new(handler, encoding) ⇒ SAX::Parser
new(handler) ⇒ SAX::Parser
new ⇒ SAX::Parser
:call-seq:
##
def initialize(doc = Nokogiri::XML::SAX::Document.new, encoding = nil) @encoding = encoding @document = doc @warned = false initialize_native unless Nokogiri.jruby? end
def parse(input, &block)
to set options on the parser context before parsing begins.
If a block is given, the underlying ParserContext object will be yielded. This can be used
[Yields]
otherwise it forwards to Parser.parse_memory.
If +input+ quacks like a readable IO object, this method forwards to Parser.parse_io,
- +input+ (String, IO) The input to parse.
[Parameters]
Parse the input, sending events to the SAX::Document at #document.
parse(input) { |parser_context| ... }
:call-seq:
##
def parse(input, &block) if input.respond_to?(:read) && input.respond_to?(:close) parse_io(input, &block) else parse_memory(input, &block) end end
def parse_file(filename, encoding = @encoding)
to set options on the parser context before parsing begins.
If a block is given, the underlying ParserContext object will be yielded. This can be used
[Yields]
parsing the input, or +nil+ for auto-detection. (default #encoding)
- +encoding+ (optional Encoding, String, nil) An Encoding or encoding name to use when
- +filename+ (String) The path to the file to be parsed.
[Parameters]
Parse a file.
parse_file(filename, encoding) { |parser_context| ... }
parse_file(filename) { |parser_context| ... }
:call-seq:
##
def parse_file(filename, encoding = @encoding) raise ArgumentError, "no filename provided" unless filename raise Errno::ENOENT unless File.exist?(filename) raise Errno::EISDIR if File.directory?(filename) ctx = related_class("ParserContext").file(filename, encoding) yield ctx if block_given? ctx.parse_with(self) end
def parse_io(io, encoding = @encoding)
to set options on the parser context before parsing begins.
If a block is given, the underlying ParserContext object will be yielded. This can be used
[Yields]
parsing the input, or +nil+ for auto-detection. (default #encoding)
- +encoding+ (optional Encoding, String, nil) An Encoding or encoding name to use when
- +io+ (IO) The readable IO object from which to read input
[Parameters]
Parse an input stream.
parse_io(io, encoding) { |parser_context| ... }
parse_io(io) { |parser_context| ... }
:call-seq:
##
def parse_io(io, encoding = @encoding) ctx = related_class("ParserContext").io(io, encoding) yield ctx if block_given? ctx.parse_with(self) end
def parse_memory(input, encoding = @encoding)
to set options on the parser context before parsing begins.
If a block is given, the underlying ParserContext object will be yielded. This can be used
[Yields]
parsing the input, or +nil+ for auto-detection. (default #encoding)
- +encoding+ (optional Encoding, String, nil) An Encoding or encoding name to use when
- +input+ (String) The input string to be parsed.
[Parameters]
Parse an input string.
parse_memory(input, encoding) { |parser_context| ... }
parse_memory(input) { |parser_context| ... }
:call-seq:
##
def parse_memory(input, encoding = @encoding) ctx = related_class("ParserContext").memory(input, encoding) yield ctx if block_given? ctx.parse_with(self) end