module MultiXml

Each MultiXml parser is expected to parse an XML document into a Hash. The conversion rules are:

Constants

CONTENT_ROOT
PARSING
REQUIREMENT_MAP
VERSION

Public Class Methods

default_parser() click to toggle source

The default parser based on what you currently have loaded and installed. First checks to see if any parsers are already loaded, then checks to see which are installed if none are loaded.

# File lib/multi_xml.rb, line 56
def default_parser
  return :libxml if defined?(::LibXML)
  return :nokogiri if defined?(::Nokogiri)
  return :ox if defined?(::Ox)

  REQUIREMENT_MAP.each do |(library, parser)|
    begin
      require library
      return parser
    rescue LoadError
      next
    end
  end
end
parse(xml, options={}) click to toggle source

Parse an XML string or IO into Ruby.

Options

:symbolize_keys

If true, will use symbols instead of strings for the keys.

# File lib/multi_xml.rb, line 95
def parse(xml, options={})
  xml ||= ''

  xml.strip! if xml.respond_to?(:strip!)
  begin
    xml = StringIO.new(xml) unless xml.respond_to?(:read)

    char = xml.getc
    return {} if char.nil?
    xml.ungetc(char)

    hash = typecast_xml_value(undasherize_keys(parser.parse(xml))) || {}
  rescue parser.parse_error => error
    raise ParseError, error.to_s, error.backtrace
  end
  hash = symbolize_keys(hash) if options[:symbolize_keys]
  hash
end
parser() click to toggle source

Get the current parser class.

# File lib/multi_xml.rb, line 46
def parser
  return @parser if @parser
  self.parser = self.default_parser
  @parser
end
parser=(new_parser) click to toggle source

Set the XML parser utilizing a symbol, string, or class. Supported by default are:

  • :libxml

  • :nokogiri

  • :ox

  • :rexml

# File lib/multi_xml.rb, line 78
def parser=(new_parser)
  case new_parser
  when String, Symbol
    require "multi_xml/parsers/#{new_parser.to_s.downcase}"
    @parser = MultiXml::Parsers.const_get("#{new_parser.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when Class, Module
    @parser = new_parser
  else
    raise "Did not recognize your parser specification. Please specify either a symbol or a class."
  end
end