Skip to content

Instantly share code, notes, and snippets.

@ronan-mch
Created June 28, 2013 07:58
Show Gist options
  • Save ronan-mch/5883180 to your computer and use it in GitHub Desktop.
Save ronan-mch/5883180 to your computer and use it in GitHub Desktop.
recursive function to render xml from a multidimensional hash using Nokogiri.
def write_xml(hash)
builder = Nokogiri::XML::Builder.new do |xml|
hash_to_xml(hash, xml)
end
render xml: builder
end
def hash_to_xml(hash, xml)
hash.each do |key, value|
if value.is_a? String
xml.send(key, value)
elsif value.is_a? Hash
xml.send(key.to_s) { hash_to_xml(value, xml) }
end
end
xml
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment