Class: FasterXmlSimple

Inherits:
Object
  • Object
show all
Defined in:
lib/faster_xml_simple.rb

Constant Summary collapse

Version =
'0.5.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options) ⇒ FasterXmlSimple

:nodoc:



57
58
59
60
# File 'lib/faster_xml_simple.rb', line 57

def initialize(string, options) #:nodoc:
  @doc     = parse(string)
  @options = default_options.merge options
end

Class Method Details

.xml_in(string, options = {}) ⇒ Object

Take an string containing XML, and returns a hash representing that XML document. For example:

FasterXmlSimple.xml_in("<root><something>1</something></root>")
{"root"=>{"something"=>{"__content__"=>"1"}}}

Faster XML Simple is designed to be a drop in replacement for the xml_in functionality of xml-simple.rubyforge.org

The following options are supported:

  • contentkey: The key to use for the content of text elements, defaults to ‘_content_

  • forcearray: The list of elements which should always be returned as arrays. Under normal circumstances single element arrays are inlined.

  • suppressempty: The value to return for empty elements, pass true to remove empty elements entirely.

  • keeproot: By default the hash returned has a single key with the name of the root element. If the name of the root element isn’t interesting to you, pass false.

  • forcecontent: By default a text element with no attributes, will be collapsed to just a string instead of a hash with a single key.

    Pass true to prevent this.



52
53
54
# File 'lib/faster_xml_simple.rb', line 52

def xml_in(string, options={})
  new(string, options).out
end

Instance Method Details

#outObject

:nodoc:



62
63
64
65
66
67
68
# File 'lib/faster_xml_simple.rb', line 62

def out #:nodoc:
  if @options['keeproot']
    {@doc.root.name => collapse(@doc.root)}
  else
    collapse(@doc.root)
  end
end