Skip to content

Instantly share code, notes, and snippets.

@GarPit
Created August 23, 2012 10:19
Show Gist options
  • Save GarPit/3435193 to your computer and use it in GitHub Desktop.
Save GarPit/3435193 to your computer and use it in GitHub Desktop.
simple xml generation
def xml
@xml_string << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
end
def tag(tag, attrs={}, &block)
@xml_string << "<#{tag}"
text = attrs.delete(:text)
@xml_string << " " if not attrs.empty?
attrs.each_pair do |key, value|
@xml_string << "#{key.to_s}=\"#{value.to_s}\""
@xml_string << " " if key != attrs.keys.last
end
@xml_string << ">"
if block_given?
block.arity < 1 ? self.instance_eval(&block) : block.call(self)
end
@xml_string << text.to_s.gsub(/[&"'<>]/) {|match| REPLACEMENTS[match]} if text
@xml_string << "</#{tag}>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment