Skip to content

Instantly share code, notes, and snippets.

@moubry
Created September 3, 2014 14:24
Show Gist options
  • Save moubry/fed0249f999e8c9d8a61 to your computer and use it in GitHub Desktop.
Save moubry/fed0249f999e8c9d8a61 to your computer and use it in GitHub Desktop.
Nokogiri::XML::Builder example
require 'nokogiri'
class TripWriter
def self.execute(xml, options)
xml.trip options
end
def self.raw_xml
'<trip><name>Movies</name><item>IMAX Movie</item></trip>'
end
end
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
xml.trips {
TripWriter.execute(xml, name: 'Honolulu')
xml << TripWriter.raw_xml
}
xml.most_relevant {
xml << TripWriter.raw_xml
}
xml.products {
xml.widget {
xml.id_ '20'
xml.blah false
xml.name 'Awesome widget A', type: 'haha'
}
begin
xml.widget {
xml.id_ '10'
fail 'sux4u'
xml.name 'Awesome widget B'
}
rescue
xml.parent.remove
end
}
}
end
puts builder.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment