Class LibXML::XML::Parser

  1. ext/libxml/libxml.c
  2. lib/libxml/parser.rb
  3. show all
Parent: Object

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

context [R]
input [R]

Public class methods

XML::Parser.document(document) → XML::Parser

Creates a new parser for the specified document.

Parameters:

document - A preparsed document.
[show source]
    # File lib/libxml/parser.rb, line 14
14:       def self.document(doc)
15:         context = XML::Parser::Context.document(doc)
16:         self.new(context)
17:       end
XML::Parser.file(path) → XML::Parser
XML::Parser.file(path, :encoding => XML::Encoding::UTF_8,
:options => XML::Parser::Options::NOENT) → XML::Parser

Creates a new parser for the specified file or uri.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

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 (|).
[show source]
    # File lib/libxml/parser.rb, line 34
34:       def self.file(path, options = {})
35:         context = XML::Parser::Context.file(path)
36:         context.encoding = options[:encoding] if options[:encoding]
37:         context.options = options[:options] if options[:options]
38:         self.new(context)
39:       end
XML::Parser.io(io) → XML::Parser
XML::Parser.io(io, :encoding => XML::Encoding::UTF_8,
:options => XML::Parser::Options::NOENT
:base_uri="http://libxml.org") → XML::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.
options - Parser options.  Valid values are the constants defined on
          XML::Parser::Options.  Mutliple options can be combined
          by using Bitwise OR (|).
[show source]
    # File lib/libxml/parser.rb, line 58
58:       def self.io(io, options = {})
59:         context = XML::Parser::Context.io(io)
60:         context.base_uri = options[:base_uri] if options[:base_uri]
61:         context.encoding = options[:encoding] if options[:encoding]
62:         context.options = options[:options] if options[:options]
63:         self.new(context)
64:       end
parser.initialize(context) → XML::Parser

Creates a new XML::Parser from the specified XML::Parser::Context.

[show source]
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_warn("Passing no parameters to XML::Parser.new is deprecated.  Pass an instance of XML::Parser::Context instead.");
    context = rb_class_new_instance(0, NULL, cXMLParserContext);
  }

  rb_ivar_set(self, CONTEXT_ATTR, context);
  return self;
}
register_error_handler (proc)
[show source]
    # File lib/libxml/parser.rb, line 91
91:       def self.register_error_handler(proc)
92:         warn('Parser.register_error_handler is deprecated.  Use Error.set_handler instead')
93:         if proc.nil?
94:           Error.reset_handler
95:         else
96:           Error.set_handler(&proc)
97:         end
98:       end
XML::Parser.string(string)
XML::Parser.string(string, :encoding => XML::Encoding::UTF_8,
:options => XML::Parser::Options::NOENT
:base_uri="http://libxml.org") → XML::Parser

Creates a new parser by parsing the specified string.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

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 (|).
[show source]
    # File lib/libxml/parser.rb, line 83
83:       def self.string(string, options = {})
84:         context = XML::Parser::Context.string(string)
85:         context.base_uri = options[:base_uri] if options[:base_uri]
86:         context.encoding = options[:encoding] if options[:encoding]
87:         context.options = options[:options] if options[:options]
88:         self.new(context)
89:       end

Public instance methods

parser.parse → XML::Document

Parse the input XML and create an XML::Document with it’s content. If an error occurs, XML::Parser::ParseError is thrown.

[show source]
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)
  {
    if (ctxt->myDoc)
      xmlFreeDoc(ctxt->myDoc);
    rxml_raise(&ctxt->lastError);
  }

  return rxml_document_wrap(ctxt->myDoc);
}