Created
June 18, 2009 16:49
-
-
Save PatrickTulskie/132002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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