class Nokogiri::XML::SAX::Parser
This parser is a SAX style parser that reads it’s input as it deems necessary. The parser takes a Nokogiri::XML::SAX::Document, an optional encoding, then given an XML input, sends messages to the Nokogiri::XML::SAX::Document.
Here is an example of using this parser:
# Create a subclass of Nokogiri::XML::SAX::Document and implement
# the events we care about:
class MyDoc < Nokogiri::XML::SAX::Document
def start_element name, attrs = []
puts "starting: #{name}"
end
def end_element name
puts "ending: #{name}"
end
end
# Create our parser
parser = Nokogiri::XML::SAX::Parser.new(MyDoc.new)
# Send some XML to the parser
parser.parse(File.open(ARGV[0]))
For more information about SAX parsers, see Nokogiri::XML::SAX. Also see Nokogiri::XML::SAX::Document for the available events.
Constants
- ENCODINGS
-
Encodinds this parser supports
Attributes
The Nokogiri::XML::SAX::Document where events will be sent.
The encoding beings used for this document.
Public Class Methods
# File lib/nokogiri/xml/sax/parser.rb, line 72 def initialize(doc = Nokogiri::XML::SAX::Document.new, encoding = "UTF-8") @encoding = check_encoding(encoding) @document = doc @warned = false end
Create a new Parser with doc and encoding
Public Instance Methods
# File lib/nokogiri/xml/sax/parser.rb, line 81
def parse(thing, &block)
if thing.respond_to?(:read) && thing.respond_to?(:close)
parse_io(thing, &block)
else
parse_memory(thing, &block)
end
end Parse given thing which may be a string containing xml, or an IO object.
# File lib/nokogiri/xml/sax/parser.rb, line 99 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
Parse a file with filename
# File lib/nokogiri/xml/sax/parser.rb, line 91 def parse_io(io, encoding = @encoding) ctx = ParserContext.io(io, ENCODINGS[check_encoding(encoding)]) yield ctx if block_given? ctx.parse_with(self) end
Parse given io
# File lib/nokogiri/xml/sax/parser.rb, line 109 def parse_memory(data) ctx = ParserContext.memory(data) yield ctx if block_given? ctx.parse_with(self) end
© 2008–2023 by Mike Dalessio, Aaron Patterson, Yoko Harada, Akinori MUSHA, John Shahid,
Karol Bucek, Sam Ruby, Craig Barnes, Stephen Checkoway, Lars Kanis, Sergio Arbeo,
Timothy Elliott, Nobuyoshi Nakada, Charles Nutter, Patrick MahoneyLicensed under the MIT License.
https://nokogiri.org/rdoc/Nokogiri/XML/SAX/Parser.html