Xml Builder

Posted by Daniel Wanja Tue, 10 Jan 2006 21:10:00 GMT

I created the new Playground category on this blog to expose various aspects regarding Ruby On Rails that I am exploring. It may raise more questions than provide answers, but in any case don’t hesitate to jump in and add your 2 cents.

The xml_builder method here after uses the render_to_string method to create some xml structure. The xml_string could as well have been in a separate .rxml file and and a simple _render statement instead of render_as_string could have saved one line of code. But hey, that’s what the playground is for!

class PlaygroundController < ApplicationController

  def xml_builder
    xml_string = <<-XML_END
        xml.test do
          xml.language(name)
          xml.description("Rocks!")
        end
    XML_END
    result = render_to_string(:inline => xml_string, :locals => { :name => 'Ruby'}, :type => :rxml) 
    render_text result
  end

end
The output:
<test>
  <language>Ruby</language>
  <description>Rocks!</description>
</test>
Comments

Leave a response

Comments