class LibXML::XML::Parser
The XML::Parser provides a tree based API for processing xml documents, in contract to XML::Reader‘s stream based api and XML::SaxParser callback based API.
As a result, parsing a document creates an in-memory document object that consist of any number of XML::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 XML::Reader.
Using the parser is simple:
parser = XML::Parser.file('my_file') doc = parser.parse
You can also parse documents (see XML::Parser.document), strings (see XML::Parser.string) and io objects (see XML::Parser.io).
Attributes
Public Class Methods
Creates a new parser for the specified document.
Parameters:
document - A preparsed document.
# File lib/libxml/parser.rb 14 def self.document(doc) 15 context = XML::Parser::Context.document(doc) 16 self.new(context) 17 end
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.
options - Parser options. Valid values are the constants defined on
XML::Parser::Options. Mutliple options can be combined
by using Bitwise OR (|).
# File lib/libxml/parser.rb 35 def self.file(path, base_uri: nil, encoding: nil, options: nil) 36 context = XML::Parser::Context.file(path) 37 context.base_uri = base_uri if base_uri 38 context.encoding = encoding if encoding 39 context.options = options if options 40 self.new(context) 41 end
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.
options - Parser options. Valid values are the constants defined on
XML::Parser::Options. Mutliple options can be combined
by using Bitwise OR (|).
# File lib/libxml/parser.rb 60 def self.io(io, base_uri: nil, encoding: nil, options: nil) 61 context = XML::Parser::Context.io(io) 62 context.base_uri = base_uri if base_uri 63 context.encoding = encoding if encoding 64 context.options = options if options 65 self.new(context) 66 end
Creates a new XML::Parser from the specified XML::Parser::Context.
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;
}
# File lib/libxml/parser.rb 93 def self.register_error_handler(proc) 94 warn('Parser.register_error_handler is deprecated. Use Error.set_handler instead') 95 if proc.nil? 96 Error.reset_handler 97 else 98 Error.set_handler(&proc) 99 end 100 end
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.
options - Parser options. Valid values are the constants defined on
XML::Parser::Options. Multiple options can be combined
by using Bitwise OR (|).
# File lib/libxml/parser.rb 85 def self.string(string, base_uri: nil, encoding: nil, options: nil) 86 context = XML::Parser::Context.string(string) 87 context.base_uri = base_uri if base_uri 88 context.encoding = encoding if encoding 89 context.options = options if options 90 self.new(context) 91 end
Public Instance Methods
Parse the input XML and create an XML::Document with it’s content. If an error occurs, XML::Parser::ParseError is thrown.
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);
}