Skip to content

Instantly share code, notes, and snippets.

@masak2009
Created March 16, 2010 21:29
Show Gist options
  • Save masak2009/334547 to your computer and use it in GitHub Desktop.
Save masak2009/334547 to your computer and use it in GitHub Desktop.
========= XML on Rails ============
sudo gem install builder
The Builder::XmlMarkup class provides methods:
cdata!(text) | Adds a CDATA section.
comment!(comment_text) | Adds a comment.
declare!(inst, *args, &block) | Adds a declaration. args specifies 0 or more arguments.
instruct!(directive_tag=:xml, attrs={}) | Adds a processing instruction. Attributes are specified with an array of hash entries.
new(options={}) | Creates an XML markup Builder object. The following options may be specified in an array of hash entries. :target=>targetObject:indent=>indentation :margin=>initial_indentation
target!() | Returns target of Builder object.
First, in the controller, remove the other bits in the format.xml line so it reads
respond_to do |format|
format.xml # index.xml.builder
end
Second, you need to create an xml.builder file, in this case, index.xml.builder.
Inside the xml.builder view, you have access to an ‘xml’ object that is used to generate the xml. For example @products:
xml.instruct!
xml.SHOP do
@products.each do |product|
xml.SHOPITEM do
xml.PRODUCT product.name
xml.DESCRIPTION product.description
end
end
end
source:
http://www.xml.com/lpt/a/1688
http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment