From 676c8d51df2a08cba4fdc3c8eebca11ac12162bc Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 23 Aug 2012 14:23:10 +0200 Subject: [PATCH] feature #1457: Instead of using root use the current path to generate the hash in XMLUtils.to_hash --- src/oca/ruby/OpenNebula/XMLUtils.rb | 37 ++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/oca/ruby/OpenNebula/XMLUtils.rb b/src/oca/ruby/OpenNebula/XMLUtils.rb index 2af0b40850..13100fafdf 100644 --- a/src/oca/ruby/OpenNebula/XMLUtils.rb +++ b/src/oca/ruby/OpenNebula/XMLUtils.rb @@ -335,12 +335,33 @@ module OpenNebula return str end - # - # - # - def to_hash(hash={}, element=nil) - element ||= @xml.document.root + # @return [Hash] a hash representing the resource + def to_hash + hash = {} + if NOKOGIRI + if @xml.instance_of?(Nokogiri::XML::NodeSet) + @xml.each { |c| + if c.element? + build_hash(hash, c) + end + } + else + build_hash(hash, @xml) + end + else + build_hash(hash, @xml) + end + + hash + end + + private + + # + # + # + def build_hash(hash, element) if NOKOGIRI array = element.children if array.length==1 and (array.first.text? or array.first.cdata?) @@ -349,14 +370,14 @@ module OpenNebula r = {} array.each { |c| if c.element? - to_hash(r, c) + build_hash(r, c) end } end else r = {} if element.has_elements? - element.each_element { |c| to_hash(r, c) } + element.each_element { |c| build_hash(r, c) } elsif element.has_text? r = element.text end @@ -376,8 +397,6 @@ module OpenNebula hash end - private - # # #