Class: LibXML::XML::SaxParser
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
| Inherits: | Object |
| Defined in: | ext/libxml/ruby_xml_sax_parser.c, ext/libxml/ruby_xml_sax_parser.c, lib/libxml/sax_callbacks.rb, lib/libxml/sax_parser.rb |
Overview
SaxParser provides a callback based API for parsing documents, in contrast to XML::Parser’s tree based API and XML::Reader’s stream based API.
The SaxParser API is fairly complex, not well standardized, and does not directly support validation making entity, namespace and base processing relatively hard.
To use the SaxParser, register a callback class via the #callbacks=. It is easiest to include the Callbacks module in your class and override the methods as needed.
Basic example:
class MyCallbacks
include XML::SaxParser::Callbacks
def on_start_element(element, attributes)
puts #Element started: #{element}"
end
end
parser = XML::SaxParser.string(my_string)
parser.callbacks = MyCallbacks.new
parser.parse
You can also parse strings (see .string) and io objects (see .io).
Class Method Summary
-
XML::SaxParser.file(path) ⇒ SaxParser
Creates a new parser by parsing the specified file or uri.
-
XML::SaxParser.io(io) ⇒ SaxParser
Creates a new reader by parsing the specified io object.
-
.new(context) ⇒ XML::Parser
constructor
Creates a new
Parserfrom the specifiedParser::Context. -
XML::SaxParser.string(string)
Creates a new parser by parsing the specified string.
Instance Attribute Summary
- #callbacks rw
Instance Method Summary
-
#parse ⇒ Boolean
Parse the input
::LibXML::XML, generating callbacks to the object registered via the #callbacks attributesibute.
Constructor Details
.new(context) ⇒ XML::Parser
Creates a new Parser from the specified Parser::Context.
# File 'ext/libxml/ruby_xml_sax_parser.c', line 53
static VALUE rxml_sax_parser_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE context = Qnil;
rb_scan_args(argc, argv, "01", &context);
if (context == Qnil)
{
rb_raise(rb_eArgError, "An instance of a XML::Parser::Context must be passed to XML::SaxParser.new");
}
rb_ivar_set(self, CONTEXT_ATTR, context);
return self;
}
Class Method Details
XML::SaxParser.file(path) ⇒ SaxParser
Creates a new parser by parsing the specified file or uri.
XML::SaxParser.io(io) ⇒ SaxParser
XML::SaxParser.io(io, :encoding) ⇒ XML::Encoding::UTF_8
SaxParser
XML::SaxParser.io(io, :encoding) ⇒ XML::Encoding::UTF_8
XML::SaxParser.string(string)
Creates a new parser by parsing the specified string.
Instance Attribute Details
#callbacks (rw)
[ GitHub ]Instance Method Details
#parse ⇒ Boolean
Parse the input ::LibXML::XML, generating callbacks to the object registered via the #callbacks attributesibute.
# File 'ext/libxml/ruby_xml_sax_parser.c', line 75
static VALUE rxml_sax_parser_parse(VALUE self)
{
VALUE context = rb_ivar_get(self, CONTEXT_ATTR);
xmlParserCtxtPtr ctxt;
Data_Get_Struct(context, xmlParserCtxt, ctxt);
ctxt->sax2 = 1;
ctxt->userData = (void*)rb_ivar_get(self, CALLBACKS_ATTR);
memcpy(ctxt->sax, &rxml_sax_handler, sizeof(rxml_sax_handler));
int status = xmlParseDocument(ctxt);
/* Now check the parsing result*/
if (status == -1 || !ctxt->wellFormed)
{
rxml_raise(&ctxt->lastError);
}
return Qtrue;
}