Class: Nokogiri::HTML::SAX::Parser

Inherits:
XML::SAX::Parser show all
Defined in:
lib/nokogiri/html/sax/parser.rb

Overview

This class lets you perform SAX style parsing on HTML with HTML error correction.

Here is a basic usage example:

class MyDoc < Nokogiri::XML::SAX::Document
  def start_element name, attributes = []
    puts "found a #{name}"
  end
end

parser = Nokogiri::HTML::SAX::Parser.new(MyDoc.new)
parser.parse(File.read(ARGV[0], 'rb'))

For more information on SAX parsers, see Nokogiri::XML::SAX

Constant Summary

Constants inherited from XML::SAX::Parser

XML::SAX::Parser::ENCODINGS

Instance Attribute Summary

Attributes inherited from XML::SAX::Parser

#cstruct, #document, #encoding

Instance Method Summary collapse

Methods inherited from XML::SAX::Parser

#initialize, new, #parse, #parse_io

Constructor Details

This class inherits a constructor from Nokogiri::XML::SAX::Parser

Instance Method Details

#parse_file(filename, encoding = 'UTF-8') ⇒ Object

Parse a file with filename

Raises:

  • (ArgumentError)


39
40
41
42
43
44
# File 'lib/nokogiri/html/sax/parser.rb', line 39

def parse_file filename, encoding = 'UTF-8'
  raise ArgumentError unless filename
  raise Errno::ENOENT unless File.exists?(filename)
  raise Errno::EISDIR if File.directory?(filename)
  ParserContext.file(filename, encoding).parse_with self
end

#parse_memory(data, encoding = 'UTF-8') ⇒ Object

Parse html stored in data using encoding

Raises:

  • (ArgumentError)


31
32
33
34
35
# File 'lib/nokogiri/html/sax/parser.rb', line 31

def parse_memory data, encoding = 'UTF-8'
  raise ArgumentError unless data
  return unless data.length > 0
  ParserContext.memory(data, encoding).parse_with self
end