Created
June 28, 2013 07:58
-
-
Save ronan-mch/5883180 to your computer and use it in GitHub Desktop.
recursive function to render xml from a multidimensional hash using Nokogiri.
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
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