Created
September 3, 2014 14:24
-
-
Save moubry/fed0249f999e8c9d8a61 to your computer and use it in GitHub Desktop.
Nokogiri::XML::Builder example
This file contains hidden or 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' | |
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