Install
sudo gem install nokogiri
Contribute
github.com/tenderlove/nokogiri

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

Nokogiri

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}"
  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.read(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

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 = Nokogiri::XML::SAX::Document.new, encoding = 'UTF-8') Show Source

Create a new Parser with doc and encoding

# File lib/nokogiri/xml/sax/parser.rb, line 70 70: def initialize doc = Nokogiri::XML::SAX::Document.new, encoding = 'UTF-8' 71: @encoding = encoding 72: @document = doc 73: @warned = false 74: 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 79 79: def parse thing, &block 80: if thing.respond_to?(:read) && thing.respond_to?(:close) 81: parse_io(thing, &block) 82: else 83: parse_memory(thing, &block) 84: end 85: end
parse_file(filename) Show Source
 

Parse a file with filename

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

Parse given io

# File lib/nokogiri/xml/sax/parser.rb, line 89 89: def parse_io io, encoding = 'ASCII' 90: @encoding = encoding 91: ctx = ParserContext.io(io, ENCODINGS[encoding]) 92: yield ctx if block_given? 93: ctx.parse_with self 94: end
parse_memory(data) Show Source
# File lib/nokogiri/xml/sax/parser.rb, line 107 107: def parse_memory data 108: ctx = ParserContext.memory data 109: yield ctx if block_given? 110: ctx.parse_with self 111: end