Class: LibXML::XML::Parser
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | ext/libxml/ruby_xml_parser.c, ext/libxml/ruby_xml_parser.c, lib/libxml/parser.rb |
Overview
The Parser provides a tree based API for processing xml documents, in contract to XML::Reader’s stream based api and SaxParser callback based API.
As a result, parsing a document creates an in-memory document object that consist of any number of Node instances. This is simple and powerful model, but has the major limitation that the size of the document that can be processed is limited by the amount of memory available. In such cases, it is better to use the Reader.
Using the parser is simple:
parser = XML::Parser.file('my_file')
doc = parser.parse
You can also parse documents (see .document), strings (see .string) and io objects (see .io).
Class Method Summary
-
XML::Parser.document(document) ⇒ Parser
Creates a new parser for the specified document.
-
XML::Parser.file(path) ⇒ Parser
Creates a new parser for the specified file or uri.
-
XML::Parser.io(io) ⇒ Parser
Creates a new parser for the specified io object.
-
.new(context) ⇒ Parser
constructor
Creates a new
Parserfrom the specifiedContext. - .register_error_handler(proc)
-
XML::Parser.string(string)
Creates a new parser by parsing the specified string.
Instance Attribute Summary
Instance Method Summary
-
#parse ⇒ XML::Document
Parse the input
::LibXML::XMLand create anDocumentwith it’s content.
Constructor Details
.new(context) ⇒ Parser
Creates a new Parser from the specified Parser::Context.
# File 'ext/libxml/ruby_xml_parser.c', line 39
static VALUE rxml_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::Parser.new");
}
rb_ivar_set(self, CONTEXT_ATTR, context);
return self;
}
Class Method Details
XML::Parser.document(document) ⇒ Parser
Creates a new parser for the specified document.
Parameters:
document - A preparsed document.
XML::Parser.file(path) ⇒ Parser
XML::Parser.file(path, encoding: XML::Encoding::UTF_8)
.options ⇒ Parser
Parser
XML::Parser.file(path, encoding: XML::Encoding::UTF_8)
.options ⇒ Parser
Creates a new parser for the specified file or uri.
Parameters:
path - Path to file
base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
are the encoding constants defined on XML::Encoding.
- Parser . Valid values are the constants defined on
XML::Parser::Options. Mutliple can be combined
by using Bitwise OR (|).
XML::Parser.io(io) ⇒ Parser
XML::Parser.io(io, encoding: XML::Encoding::UTF_8)
.options
.base_uri ⇒ Parser
Parser
XML::Parser.io(io, encoding: XML::Encoding::UTF_8)
.options
.base_uri ⇒ Parser
Creates a new parser for the specified io object.
Parameters:
io - io object that contains the xml to parser
base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
are the encoding constants defined on XML::Encoding.
- Parser . Valid values are the constants defined on
XML::Parser::Options. Mutliple can be combined
by using Bitwise OR (|).
.register_error_handler(proc)
[ GitHub ]# File 'lib/libxml/parser.rb', line 93
def self.register_error_handler(proc) warn('Parser.register_error_handler is deprecated. Use Error.set_handler instead') if proc.nil? Error.reset_handler else Error.set_handler(&proc) end end
XML::Parser.string(string)
XML::Parser.string(string, encoding: XML::Encoding::UTF_8)
.options
.base_uri ⇒ Parser
Parser
Creates a new parser by parsing the specified string.
Parameters:
string - The string to parse
base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
are the encoding constants defined on XML::Encoding.
- Parser . Valid values are the constants defined on
XML::Parser::Options. Multiple can be combined
by using Bitwise OR (|).
Instance Attribute Details
#context (readonly)
[ GitHub ]#input (readonly)
[ GitHub ]Instance Method Details
#parse ⇒ XML::Document
Parse the input ::LibXML::XML and create an Document with it’s content. If an error occurs, XML::Parser::ParseError is thrown.
# File 'ext/libxml/ruby_xml_parser.c', line 62
static VALUE rxml_parser_parse(VALUE self)
{
xmlParserCtxtPtr ctxt;
VALUE context = rb_ivar_get(self, CONTEXT_ATTR);
Data_Get_Struct(context, xmlParserCtxt, ctxt);
if ((xmlParseDocument(ctxt) == -1 || !ctxt->wellFormed) && ! ctxt->recovery)
{
rxml_raise(&ctxt->lastError);
}
rb_funcall(context, rb_intern("close"), 0);
return rxml_document_wrap(ctxt->myDoc);
}