Over a million developers have joined DZone.

XML Attributes To Database Columns

DZone's Guide to

XML Attributes To Database Columns

Free Resource
I recently needed to update a database with the contents of an XML file under a controlled environment (details at Blackrat's Blog) and came up with this. 

With an XML input file of the following format



and a database which contains identical columns name,desc,other (or a superset of the tags) you can use the follow snippet to populate it. 

View [import_xml.rhtml]

Import XML

<%= form_tag({ :action => 'import_xml'}, { :multipart => true }) %> <%= file_field 'document', 'file' %> <%= submit_tag 'Import' %> <%= end_form_tag %>
Controller [names_controller.rb] def import_xml require 'rexml/document' file=params[:document][:file] doc=REXML::Document.new(file.read) doc.root.each_element('//tag') do |tag| @name = Name.new @name.update_attributes(tag.attributes) end redirect_to :action => 'list' end

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}