Module: Crackr::XML
- Defined in:
- lib/crackr/xml.rb
Class Method Summary collapse
-
.parse(xml) ⇒ Hash
Builds a hash from an XML document.
Class Method Details
.parse(xml) ⇒ Hash
Builds a hash from an XML document.
Nokogiri XML document, an element thereof, or a string representation of an XML document.
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 44 45 46 47 48 49 50 |
# File 'lib/crackr/xml.rb', line 12 def self.parse(xml) case xml when String parse Nokogiri::XML(xml) when Nokogiri::XML::Document parse 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 = parse 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 |