Module: DataBindings::Adapters::XML

Includes:
Ruby, GemRequirement
Defined in:
lib/data_bindings/adapters/xml.rb

Defined Under Namespace

Modules: Convert

Instance Method Summary collapse

Methods included from GemRequirement

gentle_require_gem, included

Methods included from Ruby

#from_ruby, #from_ruby_array, #from_ruby_hash

Instance Method Details

#from_xml(str) ⇒ RubyObjectAdapter, RubyArrayAdapter

Constructs a wrapped object from a JSON string

Parameters:

  • str (String)

    The JSON object

Returns:



10
11
12
# File 'lib/data_bindings/adapters/xml.rb', line 10

def from_xml(str)
  from_ruby(from_xml_obj(Nokogiri::XML(str)))
end

#from_xml_obj(o) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/data_bindings/adapters/xml.rb', line 15

def from_xml_obj(o)
  case o.type
  when Nokogiri::XML::Node::DOCUMENT_NODE
    from_xml_obj(o.children[0])
  when Nokogiri::XML::Node::TEXT_NODE
    o.text
  when Nokogiri::XML::Node::ELEMENT_NODE
    if o.children.size == 1 and o.children[0].text?
      from_xml_obj(o.children[0])
    elsif o.children[0].name == '0'
      o.children.map{ |c| from_xml_obj(c) }
    else
      Hash[o.children.map { |n| [n.name, from_xml_obj(n)] }]
    end
  end
end