# Need to generate xml to be stored in a database

**URL:** <https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442>\
**Category:** Rails\
**Created:** [November 3, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442 "2008-11-03T23:43:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 3, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/1 "2008-11-03T23:43:20Z")

</div>

Forgive my ignorance, but I have searched for a while and cannot find  
anything that would help me move in the right direction.  
I need to generate xml that will then be recored in the database so  
that I can pull out at a later date.  
Every bit of info I could find shows how to create the xml and display  
it through a .rxml view.

I also have to be able to assemble the data points for the xml from  
several different tables (I basically need to pull all of the has\_one  
and has\_many relationships for a particular table and construct the  
xml with them).

A push in the right direction would be awesome.

Thanks.

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 3, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/2 "2008-11-03T23:45:40Z")

</div>

On Nov 3, 9:51 pm, “casey.ellett” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Forgive my ignorance, but I have searched for a while and cannot find  
> anything that would help me move in the right direction.  
> I need to generate xml that will then be recored in the database so  
> that I can pull out at a later date.  
> Every bit of info I could find shows how to create the xml and display  
> it through a .rxml view.  
> Check out the builder library - it’s what rxml templates use.

Fred

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 4, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/3 "2008-11-04T15:20:21Z")

</div>

On 4 Nov 2008, at 13:54, Casey Ellett wrote:

> I have successfully been able to create the xml I needed as a view,  
> but how do I go from that to storing it as a record in the database?

Well, having generated the xml, what’s stopping you assigning it to  
some attribute of some ActiveRecord object ?

Fred

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 4, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/4 "2008-11-04T14:55:37Z")

</div>

I have successfully been able to create the xml I needed as a view, but  
how  
do I go from that to storing it as a record in the database?

On Mon, Nov 3, 2008 at 5:44 PM, Frederick C.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 4, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/5 "2008-11-04T16:16:45Z")

</div>

My complete lack of knowledge of how I would do that. I am very new to  
this.  
Can I handle that in the method I’ve put in the controller to collect  
the  
data and render the xml?

Currently I am just pulling in a specific category object in the  
controller  
and specifying a view:

def cat\_xml  
@category = Category.find(params[:id])

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :action => "cat.xml.builder", :layout => 

```

false }  
end

end

And I have the xml Builder template in:

cat\_xml.xml.builder

Where would I assign this view to an ActiveRecord object?

Or am I going down the wrong path here? I don’t actually need to view  
the  
xml at any point - I’m actually going to pull it out of the database at  
a  
later point and send it up to a web service. Or I guess I could send it  
as  
it’s generated, I just want to save it to pull it later if need be.

Thanks for taking an interest in helping me out, by the way.

On Tue, Nov 4, 2008 at 9:19 AM, Frederick C.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 4, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/6 "2008-11-04T18:03:49Z")

</div>

On Tue, Nov 4, 2008 at 11:09 AM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> > Or am I going down the wrong path here? I don’t actually need to  
> > view the xml at any point - I’m actually going to pull it out of the  
> > database at a later point and send it up to a web service. Or I  
> > guess I could send it as it’s generated, I just want to save it to  
> > pull it later if need be.

We do this very same thing. It may or may not be the best solution, but  
we  
store the xml in a temp file on the server, and then have the path  
listed in  
our MySQL database as text. We just read the whole thing and pass to the  
web  
service when we’re ready to move on.

Our process goes like this:

1. Customer creates a cart and checks out.
2. Call web services to generate the completed order xml.
3. Save order xml into temp text file and put the path into the db.
4. Display confirmation page with totals/taxes (generated from web  
service).
5. If confirmed, read order xml and call save method in web service.

It’s simple and doesn’t require a whole bunch of coding. It also gives  
you  
a nice history to look at if there are any problems with the xml.

We remove the xml temp file once the order has been completed so it  
doesn’t  
eat all of our server space.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 4, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/7 "2008-11-04T20:31:57Z")

</div>

I’ll give this a go tonight. Thanks.

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 5, 2008, 5:55am UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/8 "2008-11-05T05:55:06Z")

</div>

I got this sorted out.

I ended up creating the new ActiveRecord object and passing the xml from  
builder into that.

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![casey.ellett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@casey.ellett](https://www.ruby-forum.com/u/casey.ellett)\
**Post date:** [November 4, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/need-to-generate-xml-to-be-stored-in-a-database/152442/9 "2008-11-04T16:43:19Z")

</div>

On 4 Nov 2008, at 15:04, Casey Ellett wrote:

> cat\_xml.xml.builder
> 
> Where would I assign this view to an ActiveRecord object?
> 
> Or am I going down the wrong path here? I don’t actually need to  
> view the xml at any point - I’m actually going to pull it out of the  
> database at a later point and send it up to a web service. Or I  
> guess I could send it as it’s generated, I just want to save it to  
> pull it later if need be.

If you don’t have a view, you don’t need a template. you can do  
something like

xml = Builder::XmlMarkup.new(:indent =\> 2)  
xml.instruct!  
… #buildery stuff here

SomeObject.create :xml\_text =\> xml.target!

Fred
