Module: Vacuum::HashBuilder
- Defined in:
- lib/vacuum/hash_builder.rb
Class Method Summary collapse
-
.from_xml(xml) ⇒ Hash
Builds a hash from a Nokogiri XML document.
Class Method Details
.from_xml(xml) ⇒ Hash
Builds a hash from a Nokogiri XML document.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vacuum/hash_builder.rb', line 7 def self.from_xml(xml) case xml when Nokogiri::XML::Document from_xml(xml.root) when Nokogiri::XML::Element hsh = {} xml.attributes.each_pair do |key, attribute| hsh[key] = attribute.value end xml.children.each do |child| result = from_xml(child) if child.name == 'text' if hsh.empty? return result else hsh['__content__'] = result end elsif hsh[child.name] case hsh[child.name] when Array hsh[child.name] << result else hsh[child.name] = [hsh[child.name]] << result end else hsh[child.name] = result end end hsh else xml.content.to_s end end |