XML output


#1

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" ?> Some text Me

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
end

Am i understanding the use of Builder right? I am new to this and would
love some feedback and/or pointers.

Thanks.


#2

Mufaddal K. wrote:

xml = Builder::XmlMarkup.new
xml.response(:status => “NOT_AVAILABLE”)
xml.message(“Some text”)
if @from != “none”
@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 R.
http://tzinfo.rubyforge.org/ – DST-aware timezone library for Ruby


#3

Mufaddal K. 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:

http://myfavouritecartoon.com/manager/view?option=x&water=yes

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 R.
http://tzinfo.rubyforge.org/ – DST-aware timezone library for Ruby


#4

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:

http://myfavouritecartoon.com/manager/view?option=x&water=yes

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

Thanks.