Hi, Sorry abt the half finished post that I sent out earlier. The idea is to generate an output xml response as such: <?xml version="1.0" encoding="ISO-8859-1" ?> <response status=''NOT_AVAILABLE''> <message>Some text</message> <from>Me</from> </response> I make a request like such http://localhost:3000/manager/shome_xml?from=Me The page i get back is blank. I have a method showme_xml in my controller manager_controller.rb class ManagerController < ApplicationController def shome_xml @from = params[:from] render :action => "showme_xml", :layout => nil end end I have got a shome_xml.rxml template in my view section. xml = Builder::XmlMarkup.new xml.response(:status => "NOT_AVAILABLE") xml.message("Some text") if @from != "none" <from>@from</from> end Am i understanding the use of Builder right? I am new to this and would love some feedback and/or pointers. Thanks. -- Posted via http://www.ruby-forum.com/.
Mufaddal Khumri wrote:> The idea is to generate an output xml response as such: > > <?xml version="1.0" encoding="ISO-8859-1" ?> > <response status=''NOT_AVAILABLE''> > <message>Some text</message> > <from>Me</from> > </response> > > I have got a shome_xml.rxml template in my view section. > > xml = Builder::XmlMarkup.new > xml.response(:status => "NOT_AVAILABLE") > xml.message("Some text") > if @from != "none" > <from>@from</from> > endThis is almost correct. The following should generate what you are after: xml.instruct! :xml, :version=>"1.0", :encoding => ''ISO-8859-1'' xml.response(:status => "NOT_AVAILABLE") { xml.message("Some text") if @from != "none" xml.from(@from) end } The XmlMarkup object gets created automatically, so your first line is unnecessary. To create child nodes under response, you have to use a block and nest the contents. To get the <?xml ?> directive, use the xml.instruct! method. -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
Thanks . After I got my syntax right it worked. The last question I have is if there is a way to escape the xml data? one of my xml nodes has a url. Something like this: <url> http://myfavouritecartoon.com/manager/view?option=x&water=yes </url> Is there a way to encode such data in rails so that the xml output is welformed? Thanks. Philip Ross wrote:> Mufaddal Khumri wrote: > >> The idea is to generate an output xml response as such: >> >> <?xml version="1.0" encoding="ISO-8859-1" ?> >> <response status=''NOT_AVAILABLE''> >> <message>Some text</message> >> <from>Me</from> >> </response> >> >> I have got a shome_xml.rxml template in my view section. >> >> xml = Builder::XmlMarkup.new >> xml.response(:status => "NOT_AVAILABLE") >> xml.message("Some text") >> if @from != "none" >> <from>@from</from> >> end > > > This is almost correct. The following should generate what you are after: > > xml.instruct! :xml, :version=>"1.0", :encoding => ''ISO-8859-1'' > > xml.response(:status => "NOT_AVAILABLE") { > xml.message("Some text") > if @from != "none" > xml.from(@from) > end > } > > The XmlMarkup object gets created automatically, so your first line is > unnecessary. To create child nodes under response, you have to use a > block and nest the contents. To get the <?xml ?> directive, use the > xml.instruct! method. > >
Mufaddal Khumri wrote:> Thanks . After I got my syntax right it worked. The last question I have > is if there is a way to escape the xml data? one of my xml nodes has a > url. Something like this: > > <url> > http://myfavouritecartoon.com/manager/view?option=x&water=yes > </url> > > Is there a way to encode such data in rails so that the xml output is > welformed?XML text nodes are properly escaped by Builder when you do something like xml.url(@url). Note that the version of Builder used by Rails 1.1 and earlier does not escape attribute values (see http://dev.rubyonrails.org/ticket/3354). This is fixed in the Rails SVN trunk. -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby