XML import Question

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

Try using Nokogiri: http://nokogiri.org/ It's a great and easy to use parser.

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

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

http://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

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

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