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.
Encodinds this parser supports
Create a new Parser with doc and encoding
# File lib/nokogiri/xml/sax/parser.rb, line 70 def initialize(doc = Nokogiri::XML::SAX::Document.new, encoding = 'ASCII') @encoding = encoding @document = doc @warned = false end
Parse given thing which may be a string containing xml, or an IO object.
# File lib/nokogiri/xml/sax/parser.rb, line 79 def parse thing if thing.respond_to?(:read) && thing.respond_to?(:close) parse_io(thing) else parse_memory(thing) end end
Parse a file with filename
# File lib/nokogiri/xml/sax/parser.rb, line 96 def parse_file filename raise ArgumentError unless filename raise Errno::ENOENT unless File.exists?(filename) raise Errno::EISDIR if File.directory?(filename) native_parse_file filename end
Parse given io
# File lib/nokogiri/xml/sax/parser.rb, line 89 def parse_io io, encoding = 'ASCII' @encoding = encoding native_parse_io io, ENCODINGS[@encoding] || ENCODINGS['ASCII'] end
Parse the document stored in data
static VALUE parse_memory(VALUE self, VALUE data)
{
xmlSAXHandlerPtr handler;
Data_Get_Struct(self, xmlSAXHandler, handler);
if(Qnil == data) rb_raise(rb_eArgError, "data cannot be nil");
xmlSAXUserParseMemory( handler,
(void *)self,
StringValuePtr(data),
RSTRING_LEN(data)
);
return data;
}
Parse the document stored in data
static VALUE native_parse_file(VALUE self, VALUE data)
{
xmlSAXHandlerPtr handler;
Data_Get_Struct(self, xmlSAXHandler, handler);
xmlSAXUserParseFile( handler,
(void *)self,
StringValuePtr(data)
);
return data;
}
Parse the document accessable via io
static VALUE native_parse_io(VALUE self, VALUE io, VALUE encoding)
{
xmlSAXHandlerPtr handler;
Data_Get_Struct(self, xmlSAXHandler, handler);
xmlCharEncoding enc = (xmlCharEncoding)NUM2INT(encoding);
xmlParserCtxtPtr sax_ctx = xmlCreateIOParserCtxt(
handler,
(void *)self,
(xmlInputReadCallback)io_read_callback,
(xmlInputCloseCallback)io_close_callback,
(void *)io,
enc
);
xmlParseDocument(sax_ctx);
xmlFreeParserCtxt(sax_ctx);
return io;
}
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.