Created
January 13, 2012 13:37
-
-
Save zenchild/1606243 to your computer and use it in GitHub Desktop.
Nokogiri parameterized Builder
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
require 'nokogiri' | |
def mkxml(b, nm, bhash) | |
b.send(nm.to_s) { | |
bhash.each_pair do |k, v| | |
if(v.is_a?(Hash)) | |
mkxml(b, k, v) | |
elsif(v.is_a?(Array)) | |
b.send(k, v.shift, v[0].keys.first => v[0].values.first) | |
else | |
b.send(k, v) | |
end | |
end | |
} | |
end | |
xmlhash = {:Rootish => ['asdf',{:id => 122}], :pelement => {:selement => {:element => 'Hello World'}}} | |
builder = Nokogiri::XML::Builder.new do |xml| | |
mkxml(xml, 'Body', xmlhash) | |
end | |
puts builder.to_xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment