Forum: Ruby on Rails XML output

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3320715fa6264e6309503d5bd089fad2?d=identicon&s=25 Mufaddal Khumri (mkhumri)
on 2006-04-27 19:48
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.
9ab183bb240ffd3a3966d5a615b4bdeb?d=identicon&s=25 Philip Ross (Guest)
on 2006-04-27 21:12
(Received via mailing list)
Mufaddal Khumri wrote:
> 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.


--
Philip Ross
http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
3320715fa6264e6309503d5bd089fad2?d=identicon&s=25 Mufaddal Khumri (mkhumri)
on 2006-04-27 22:07
(Received via mailing list)
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=...
</url>

Is there a way to encode such data in rails so that the xml output is
welformed?

Thanks.
9ab183bb240ffd3a3966d5a615b4bdeb?d=identicon&s=25 Philip Ross (Guest)
on 2006-04-27 22:53
(Received via mailing list)
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=...
> </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
This topic is locked and can not be replied to.