### Home Classes Methods

### In Files

- lib/nokogiri/xml/sax/push_parser.rb
- lib/nokogiri/xml/sax/parser.rb
- lib/nokogiri/xml/sax/parser_context.rb
- lib/nokogiri/xml/sax/document.rb
- ext/nokogiri/html_document.c

### Namespace

- CLASS Nokogiri::XML::SAX::Document
- CLASS Nokogiri::XML::SAX::Parser
- CLASS Nokogiri::XML::SAX::ParserContext
- CLASS Nokogiri::XML::SAX::PushParser

### Files

- CHANGELOG.ja.rdoc
- CHANGELOG.rdoc
- C_CODING_STYLE.rdoc
- Manifest.txt
- README.ja.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Nokogiri
- Nokogiri::CSS
- Nokogiri::CSS::Node
- Nokogiri::CSS::Parser
- Nokogiri::CSS::SyntaxError
- Nokogiri::CSS::Tokenizer
- Nokogiri::CSS::Tokenizer::ScanError
- Nokogiri::Decorators
- Nokogiri::Decorators::Slop
- Nokogiri::EncodingHandler
- Nokogiri::HTML
- Nokogiri::HTML::Builder
- Nokogiri::HTML::Document
- Nokogiri::HTML::Document::EncodingReader
- Nokogiri::HTML::Document::EncodingReader::JumpSAXHandler
- Nokogiri::HTML::DocumentFragment
- Nokogiri::HTML::ElementDescription
- Nokogiri::HTML::EntityDescription
- Nokogiri::HTML::EntityLookup
- Nokogiri::HTML::SAX
- Nokogiri::HTML::SAX::Parser
- Nokogiri::HTML::SAX::ParserContext
- Nokogiri::HTML::SAX::PushParser
- Nokogiri::SyntaxError
- Nokogiri::XML
- Nokogiri::XML::Attr
- Nokogiri::XML::AttributeDecl
- Nokogiri::XML::Builder
- Nokogiri::XML::CDATA
- Nokogiri::XML::CharacterData
- Nokogiri::XML::Comment
- Nokogiri::XML::DTD
- Nokogiri::XML::Document
- Nokogiri::XML::DocumentFragment
- Nokogiri::XML::Element
- Nokogiri::XML::ElementContent
- Nokogiri::XML::ElementDecl
- Nokogiri::XML::EntityDecl
- Nokogiri::XML::EntityReference
- Nokogiri::XML::Namespace
- Nokogiri::XML::Node
- Nokogiri::XML::Node::SaveOptions
- Nokogiri::XML::NodeSet
- Nokogiri::XML::Notation
- Nokogiri::XML::PP
- Nokogiri::XML::PP::CharacterData
- Nokogiri::XML::PP::Node
- Nokogiri::XML::ParseOptions
- Nokogiri::XML::ProcessingInstruction
- Nokogiri::XML::Reader
- Nokogiri::XML::RelaxNG
- Nokogiri::XML::SAX
- Nokogiri::XML::SAX::Document
- Nokogiri::XML::SAX::Parser
- Nokogiri::XML::SAX::Parser::Attribute
- Nokogiri::XML::SAX::ParserContext
- Nokogiri::XML::SAX::PushParser
- Nokogiri::XML::Schema
- Nokogiri::XML::SyntaxError
- Nokogiri::XML::Text
- Nokogiri::XML::XPath
- Nokogiri::XML::XPath::SyntaxError
- Nokogiri::XML::XPathContext
- Nokogiri::XSLT
- Nokogiri::XSLT::Stylesheet
- XSD
- XSD::XMLParser
- XSD::XMLParser::Nokogiri
- Object

No matching classes.

# Nokogiri::XML::SAX

SAX Parsers are event driven parsers. Nokogiri provides two different event based parsers when dealing with XML. If you want to do SAX style parsing using HTML, check out Nokogiri::HTML::SAX.

The basic way a SAX style parser works is by creating a parser, telling the parser about the events we're interested in, then giving the parser some XML to process. The parser will notify you when it encounters events your said you would like to know about.

To register for events, you simply subclass Nokogiri::XML::SAX::Document, and implement the methods for which you would like notification.

For example, if I want to be notified when a document ends, and when an element starts, I would write a class like this:

    class MyDocument < Nokogiri::XML::SAX::Document
      def end_document
        puts "the document has ended"
      end

      def start_element name, attributes = []
        puts "#{name} started"
      end
    end

Then I would instantiate a SAX parser with this document, and feed the parser some XML

    # Create a new parser
    parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)

    # Feed the parser some XML
    parser.parse(File.open(ARGV[0]))

Now my document handler will be called when each node starts, and when then document ends. To see what kinds of events are available, take a look at Nokogiri::XML::SAX::Document.

Two SAX parsers for XML are available, a parser that reads from a string or IO object as it feels necessary, and a parser that lets you spoon feed it XML. If you want to let Nokogiri deal with reading your XML, use the Nokogiri::XML::SAX::Parser. If you want to have fine grain control over the XML input, use the Nokogiri::XML::SAX::PushParser.

[Validate]

Generated with the Darkfish Rdoc Generator 2.