sudo gem install nokogiri

An HTML, XML, SAX, & Reader parser with the ability to search documents via XPath or CSS3 selectors… and much more


Class Nokogiri::XML::SAX::Parser inherits from Object

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}"

def end_element name
  puts "ending: #{name}"

# Create our parser
parser =

# Send some XML to the parser

For more information about SAX parsers, see Nokogiri::XML::SAX. Also see Nokogiri::XML::SAX::Document for the available events.



Encodinds this parser supports


document RW

The Nokogiri::XML::SAX::Document where events will be sent.

encoding RW

The encoding beings used for this document.

Public Class Methods

new(doc =, encoding = 'UTF-8') Show Source

Create a new Parser with doc and encoding

# File lib/nokogiri/xml/sax/parser.rb, line 70 def initialize doc =, encoding = 'UTF-8' check_encoding(encoding) @encoding = encoding @document = doc @warned = false end

Public Instance Methods

parse(thing, &block) Show Source

Parse given thing which may be a string containing xml, or an IO object.

# File lib/nokogiri/xml/sax/parser.rb, line 80 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_file(filename) Show Source

Parse a file with filename

# File lib/nokogiri/xml/sax/parser.rb, line 100 def parse_file filename raise ArgumentError unless filename raise Errno::ENOENT unless File.exists?(filename) raise Errno::EISDIR if ctx = ParserContext.file filename yield ctx if block_given? ctx.parse_with self end
parse_io(io, encoding = 'ASCII') Show Source

Parse given io

# File lib/nokogiri/xml/sax/parser.rb, line 90 def parse_io io, encoding = 'ASCII' check_encoding(encoding) @encoding = encoding ctx =, ENCODINGS[encoding]) yield ctx if block_given? ctx.parse_with self end
parse_memory(data) Show Source
# 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