lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.4.2 vs lib/apricoteatsgorilla.rb in smacks-apricoteatsgorilla-0.4.3
- old
+ new
@@ -72,21 +72,14 @@
# xml = "<apricot><eats><lotsOf>Gorillas</lotsOf></eats></apricot>"
# ApricotEatsGorilla[xml, "//eats"]
# # => { :lots_of => "Gorillas" }
def xml_to_hash(xml, root_node = nil)
doc = Hpricot.XML clean_xml(xml)
- root = root_node ? doc.search(root_node) : doc.root
+ root = root_node ? doc.at(root_node) : doc.root
- return nil if root.nil?
- # root is an Hpricot::Elements object
- if root.respond_to?(:count)
- # root is an Hpricot::Elements object with only one element
- return root.entries.map { |e| xml_node_to_hash(e) } if root.count > 1
- root = root.entries.first
- if root.children.size == 1 && root.children.first.text?
- # root contains text only
- return root.children.first.to_s
- end
+ return nil if root.nil? # root_node not found
+ if root.children.size == 1 && root.children.first.kind_of?(Hpricot::Text)
+ return root.children.first.to_s # text-only value
end
xml_node_to_hash(root)
end
# Converts a given Ruby Hash into an XML String. Converts Hash keys from
\ No newline at end of file