# Database to xml for data migration

**URL:** <https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605>\
**Category:** Ruby\
**Created:** [December 10, 2007, 2:57am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605 "2007-12-10T02:57:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![damnpenguins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a88e57/32.png) [@damnpenguins](https://www.ruby-forum.com/u/damnpenguins)\
**Post date:** [December 10, 2007, 2:57am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605/1 "2007-12-10T02:57:38Z")

</div>

Hi Everyone,

I have searched google for answers and have gone down a few paths of  
investigation… But being a newbie I am rapidly getting confused and  
this should essentially be a very simple task.

What I need is some advice on the best way to go about this problem.

We are needing to generate XML documents for a migration into a new  
system from an existing database.

I looked into the ruby builder library, but couldn’t see too many  
examples outside of rails to build the XML up from a template. I know I  
can put together a rails app to use an rxml template etc, but going down  
that path seems to much effort for this…

I have a template of the xml I need to upload, what I need is to  
populate that template for each db record. What is the simplest, best  
ruby way to export from the db into xml?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![damnpenguins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a88e57/32.png) [@damnpenguins](https://www.ruby-forum.com/u/damnpenguins)\
**Post date:** [December 10, 2007, 3:33am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605/2 "2007-12-10T03:33:02Z")

</div>

A rails application would be the most suitable to generate an XML from  
database.

[http://www.xml.com/pub/a/2007/01/17/making-xml-in-a-rails-app-xml-builder.html](http://www.xml.com/pub/a/2007/01/17/making-xml-in-a-rails-app-xml-builder.html)  
Cameron S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Hi Everyone,

I have searched google for answers and have gone down a few paths of  
investigation… But being a newbie I am rapidly getting confused and  
this should essentially be a very simple task.

What I need is some advice on the best way to go about this problem.

We are needing to generate XML documents for a migration into a new  
system from an existing database.

I looked into the ruby builder library, but couldn’t see too many  
examples outside of rails to build the XML up from a template. I know I  
can put together a rails app to use an rxml template etc, but going down  
that path seems to much effort for this…

I have a template of the xml I need to upload, what I need is to  
populate that template for each db record. What is the simplest, best  
ruby way to export from the db into xml?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![damnpenguins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a88e57/32.png) [@damnpenguins](https://www.ruby-forum.com/u/damnpenguins)\
**Post date:** [December 10, 2007, 3:57am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605/3 "2007-12-10T03:57:01Z")

</div>

XML output may be generated from database query using  
DBI::Utils::XMLFormatter module.

Refer Listing 5.  
[http://www.oracle.com/technology/pub/articles/marx-ruby.html](http://www.oracle.com/technology/pub/articles/marx-ruby.html)

Cameron S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Hi Everyone,

I have searched google for answers and have gone down a few paths of  
investigation… But being a newbie I am rapidly getting confused and  
this should essentially be a very simple task.

What I need is some advice on the best way to go about this problem.

We are needing to generate XML documents for a migration into a new  
system from an existing database.

I looked into the ruby builder library, but couldn’t see too many  
examples outside of rails to build the XML up from a template. I know I  
can put together a rails app to use an rxml template etc, but going down  
that path seems to much effort for this…

I have a template of the xml I need to upload, what I need is to  
populate that template for each db record. What is the simplest, best  
ruby way to export from the db into xml?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![damnpenguins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a88e57/32.png) [@damnpenguins](https://www.ruby-forum.com/u/damnpenguins)\
**Post date:** [December 10, 2007, 7:25am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605/4 "2007-12-10T07:25:28Z")

</div>

Great - thanks for the responses!

I had mostly discounted the rails option but it seems to be the best way  
to control the actual output, as there is a bit of processing that needs  
to be done on the data.

I just wanted to make sure I wasn’t missing the easy way to do this  
rather than wrap a framework like rails around a simple db export ruby  
script!

thanks again.

---

<div class="post-metadata">

**Author:** ![damnpenguins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a88e57/32.png) [@damnpenguins](https://www.ruby-forum.com/u/damnpenguins)\
**Post date:** [December 10, 2007, 4:00am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605/5 "2007-12-10T04:00:31Z")

</div>

require “rexml/document” ;include REXML

@Skeleton = \<\<EOF  
  
EOF

someItems= REXML::Document.new(@Skeleton)

# Then you could add an/some elements:

# Iterate here

someItems.elements[’//anItem’].text = “someThing”

@drTransactionFile = File.open( “someXML.xml”, “w”)  
@drTransactionFile.write("" \<\< someItems.to\_s \<\<  
“”); @drTransactionFile.close

Not necessarily fast, maybe simple, doubtfully best.

MarkT

---

<div class="post-metadata">

**Author:** ![damnpenguins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a88e57/32.png) [@damnpenguins](https://www.ruby-forum.com/u/damnpenguins)\
**Post date:** [December 10, 2007, 11:17am UTC](https://www.ruby-forum.com/t/database-to-xml-for-data-migration/122605/6 "2007-12-10T11:17:40Z")

</div>

2007/12/10, cs ss [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Great - thanks for the responses!
> 
> I had mostly discounted the rails option but it seems to be the best way  
> to control the actual output, as there is a bit of processing that needs  
> to be done on the data.
> 
> I just wanted to make sure I wasn’t missing the easy way to do this  
> rather than wrap a framework like rails around a simple db export ruby  
> script!

Well, if it is just a single and static template you could even use  
string interpolation to do the job. Certainly faster than REXML but  
YMMV.

Kind regards

robert
