# XML output

**URL:** <https://www.ruby-forum.com/t/xml-output/94282>\
**Category:** Rails\
**Created:** [April 27, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/xml-output/94282 "2006-04-27T19:48:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [April 27, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/xml-output/94282/1 "2006-04-27T19:48:14Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [April 27, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/xml-output/94282/2 "2006-04-27T21:12:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [April 27, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/xml-output/94282/3 "2006-04-27T22:53:03Z")

</div>

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](http://dev.rubyonrails.org/ticket/3354)).  
This is fixed in the Rails SVN trunk.

–  
Philip R.  
[http://tzinfo.rubyforge.org/](http://tzinfo.rubyforge.org/) – DST-aware timezone library for Ruby

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [April 27, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/xml-output/94282/4 "2006-04-27T22:07:43Z")

</div>

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.
