# XML import Question

**URL:** <https://www.ruby-forum.com/t/xml-import-question/185148>\
**Category:** Rails\
**Created:** [March 15, 2010, 10:02am UTC](https://www.ruby-forum.com/t/xml-import-question/185148 "2010-03-15T10:02:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![burguer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@burguer](https://www.ruby-forum.com/u/burguer)\
**Post date:** [March 15, 2010, 10:02am UTC](https://www.ruby-forum.com/t/xml-import-question/185148/1 "2010-03-15T10:02:56Z")

</div>

hi guys can somebody help to find some tutorial or link to find the way  
to import an external XML file to rails with some nodes and child-nodes.

Thanks

---

<div class="post-metadata">

**Author:** ![burguer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@burguer](https://www.ruby-forum.com/u/burguer)\
**Post date:** [March 15, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/xml-import-question/185148/2 "2010-03-15T14:55:47Z")

</div>

Try using Nokogiri: [http://nokogiri.org/](http://nokogiri.org/) It’s a great and easy to use  
parser.

---

<div class="post-metadata">

**Author:** ![burguer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@burguer](https://www.ruby-forum.com/u/burguer)\
**Post date:** [March 16, 2010, 12:38am UTC](https://www.ruby-forum.com/t/xml-import-question/185148/3 "2010-03-16T00:38:52Z")

</div>

What are you after? The answer really depends on your situation.

Two options come to mind:

A custom rake task. You can see a simple example here that uses YAML  
instead of XML as source data:

[http://github.com/kete/kete/blob/master/lib/tasks/user\_export\_import.rake](http://github.com/kete/kete/blob/master/lib/tasks/user_export_import.rake)

Here’s an example of parsing from source XML (modify this and replace  
the YAML parsing the above rake task):

> <https://github.com/kete/kete/blob/master/lib/workers/excel_based_importer_worker.rb>

- starting around line 30, note the hardcoding of element names that  
you need to adjust

Another option, if you have already created a RESTful interface that  
accepts XML for creating new records, you might simply create a script  
that parses your existing XML file and submits a POST to your create  
action with the relevant data. Not necessarily the fastest process,  
but if it is a one off import it might be the quickest thing to put  
together programming wise. Net::HTTP and Nokogiri seem like all you  
really need.

I would probably just go with the rake task. Pretty simple.

Cheers,  
Walter

---

<div class="post-metadata">

**Author:** ![burguer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@burguer](https://www.ruby-forum.com/u/burguer)\
**Post date:** [March 16, 2010, 3:09am UTC](https://www.ruby-forum.com/t/xml-import-question/185148/4 "2010-03-16T03:09:24Z")

</div>

Thanks a lot for your help i’ll try with thats options  
have you guys ever used the "Hash.from\_xml " method to import XML

thanks for the help i really really appreciate

---

<div class="post-metadata">

**Author:** ![burguer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@burguer](https://www.ruby-forum.com/u/burguer)\
**Post date:** [March 16, 2010, 3:43am UTC](https://www.ruby-forum.com/t/xml-import-question/185148/5 "2010-03-16T03:43:12Z")

</div>

On Tue, Mar 16, 2010 at 3:09 PM, Burguer B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks a lot for your help i’ll try with thats options  
> have you guys ever used the "Hash.from\_xml " method to import XML

I’ve used it in the past. If you aren’t importing a huge amount of  
records it might be ok. Otherwise I think you are better off using  
Nokogiri. Depends on the complexity of your source XML.

Cheers,  
Walter
