MultiXml

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

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 82
def default_parser
  return :ox if defined?(::Ox)
  return :libxml if defined?(::LibXML)
  return :nokogiri if defined?(::Nokogiri)

  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.

:disallowed_types

Types to disallow from being typecasted. Defaults to `['yaml', 'symbol']`. Use `[]` to allow all types.

:typecast_xml_value

If true, won't typecast values for parsed document

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

  options = DEFAULT_OPTIONS.merge(options)

  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 = undasherize_keys(parser.parse(xml) || {})
    hash = options[:typecast_xml_value] ? typecast_xml_value(hash, options[:disallowed_types]) : hash
  rescue DisallowedTypeError
    raise
  rescue parser.parse_error => error
    raise ParseError, error.message, 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 72
def parser
  return @@parser if defined?(@@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 104
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.