Each MultiXml parser is expected to parse an XML document into a Hash. The conversion rules are:
Each document starts out as an empty Hash.
Reading an element created an entry in the parent Hash that has a key of the element name and a value of a Hash with attributes as key value pairs. Children are added as described by this rule.
Text and CDATE is stored in the parent element Hash with a key of '__content__' and a value of the text itself.
If a key already exists in the Hash then the value associated with the key is converted to an Array with the old and new value in it.
Other elements such as the xml prolog, doctype, and comments are ignored.
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 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
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
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
Generated with the Darkfish Rdoc Generator 2.