views:

135

answers:

1

As you all know, with Rails it is possible to use Builder::XMLMarkup templates to provide an http reponse in XML format instead of HTML (with the respond_to command). My problem is that I would like to use the Builder::XMLMarkup templating system not with Rails but with Ruby only (i.e. a standalone program that generates/outputs an XML file from an XML template). The question is then twofold:

  1. How do I tell the Ruby program which is the template I want to use? and
  2. How do I tell the Builder class which is the output XML file ?

There is already a similar answer to that in Stackoverflow (http://stackoverflow.com/questions/2176287/how-do-i-generate-xml-from-xmlbuilder-using-a-xml-builder-file), but I am afraid it is only valid for Rails.

Thanks a lot in advance

Miquel

+1  A: 

Ever read the Builder RDocs? rubyforge.org

Well, let's see if I can help a bit more. Here's a very contrived example.

require 'builder'

@received_data = {:books => [{:book => { :author => "John Doe", :title => "Doeisms" }}, {:book => { :author => "Jane Doe", :title => "Doeisms II" }}]}
@output = ""
xml = Builder::XMLMarkup.new(:target => @output, :ident => 1)

xml.instruct!
xml.books do
  @received_data[:books].each do |book|
    xml.book do
      xml.title book[:title]
      xml.author book[:author]
    end
  end
end

The @output object will contain your xml markup. To select a specific template, you could pass arguments to your program for this decision.
Anyway, I prefer to use libxml-ruby to parse and build XML documents, but that's a matter of taste.

Mereghost
Yes I have read it but I could not see any mention in how to actually create an XML using Builder templates with Ruby (not Rails)...
Tryskele
I've expanded somewhat the answer. Take a look.
Mereghost