# XML question

**URL:** <https://www.ruby-forum.com/t/xml-question/65081>\
**Category:** Rails\
**Created:** [August 16, 2006, 12:27am UTC](https://www.ruby-forum.com/t/xml-question/65081 "2006-08-16T00:27:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [August 16, 2006, 12:27am UTC](https://www.ruby-forum.com/t/xml-question/65081/1 "2006-08-16T00:27:08Z")

</div>

Is there a way you can use RoR to create an .xml file?

Say I had a DB full of info that I wanted to format and have export as  
“lorem.xml” inside of a specific directory in my web server?

Then, each time I updated the DB it would re-export the file (delete the  
old, save the new?)

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [August 16, 2006, 2:31am UTC](https://www.ruby-forum.com/t/xml-question/65081/2 "2006-08-16T02:31:11Z")

</div>

Hi Mason,

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [August 16, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/xml-question/65081/3 "2006-08-16T16:36:41Z")

</div>

Bill,

I found how i could generate an .xml file on the fly with .rxml, much  
like i can generate an .html file with .rhtml.

While this will create the file I need, it only exists momentarily and  
then dissapears leaving me with only a .rxml once the RoR actions are  
done. What I would like to do is have the .xml export to a separate  
location so that it can be used by another application.

The RoR is there to act as an CRUD for the XML info. I want my client to  
be able to add/edit/delete items from its XML file.

Is it possible to set up the .xml file to save in a specified directory  
once the .rxml pieces it together into a .xml?

thanks,  
mk

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [August 16, 2006, 2:46am UTC](https://www.ruby-forum.com/t/xml-question/65081/4 "2006-08-16T02:46:11Z")

</div>

Bill W. fat-fingered:

> Hi Mason,
> 
> Mason K. wrote:
> 
> > Is there a way you can use RoR to create an .xml file?

There are several options. The choice depends mainly on the complexity  
of  
what you’re trying to do. Builder and REXML will do 98% of anything  
you’d  
want to do and they’re both part of the standard distro.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [August 16, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/xml-question/65081/5 "2006-08-16T16:56:37Z")

</div>

Mason,

Don’t forget that ActiveRecord objects have a .to\_xml method that  
will output xml. You can then write that out to a file if necessary.

-Anthony

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [August 16, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/xml-question/65081/6 "2006-08-16T19:06:33Z")

</div>

Mason K. wrote:

> I found how i could generate an .xml file on the fly  
> with .rxml, much like i can generate an .html file with  
> .rhtml.
> 
> While this will create the file I need, it only exists  
> momentarily and then dissapears leaving me with  
> only a .rxml once the RoR actions are done. What  
> I would like to do is have the .xml export to a separate  
> location so that it can be used by another application.

One option, if you’re sure you just want to save what you’re already  
rendering, might be to render\_to\_string and then save that to a .xml  
file.  
Might be worth a shot, but I wouldn’t be surprised if it didn’t work for  
one  
reason or another.

> The RoR is there to act as an CRUD for the XML info.  
> I want my client to be able to add/edit/delete items from  
> its XML file.

To make sure I read this right… you don’t mean you want to edit the  
XML  
file natively do you? If so, I think that would be pretty far outside  
the  
sweet-spot for RoR.

If I understand what you mean by "RoR is there to act as… ", then  
you’re  
probably thinking along the lines of what I’m doing. My RoR app  
captures  
data from the visitor into MySQL (in my case). When they want to save  
the  
data they’ve entered, I use Builder to create an XML file from the data  
stored in MySQL. If they want to update an existing file, I use REXML to  
bring the data stored in the XML file (back) into MySQL, etc… That is  
pretty much dead center of the RoR sweet spot wrt XML, IMO.

> Is it possible to set up the .xml file to save in a specified  
> directory once the .rxml pieces it together into a .xml?

Independent of how you get there, you’re going to end up just doing a  
routine file save. Nothing magic there.

hth,  
Bill
