Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Created June 18, 2009 16:49
Show Gist options
  • Save PatrickTulskie/132002 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/132002 to your computer and use it in GitHub Desktop.
module Hpricot
module Hextensions
def hashify(xml_data)
hsh = {}
xml_data.each_child do |b|
key_name = b.name.strip
if key_name.length > 0
data = Hpricot.XML(b.inner_html)
data = (data.children && (data.children.length > 1)) ? data.to_hash : b.inner_html.strip
if hsh[key_name] && !hsh[key_name].is_a?(Array)
hsh[key_name] = [ hsh[key_name] ] # Convert it into an array...
hsh[key_name] << data
elsif hsh[key_name] && hsh[key_name].is_a?(Array)
hsh[key_name] << data
else
hsh[key_name] = data
end
end
end
return hsh
end
end
class Doc
include Hextensions
def to_hash
hashify(self)
end
end
class Elem
include Hextensions
def to_hash
hashify(self)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment