How do I open an uploaded xml in controller?

Hello,

This seems like a fairly simple solution, but I keep running into
roadblocks. I’m using a small form to select an xml from the user’s
local file system, and then parsing the information out using REXML.
The form works fine, but I can’t seem to get the body of the xml doc
opened with in the controller. I’ve included the code snippets below
for view and controller, can anyone see what i’m missing/doing wrong?

thanks,
Dave

view:
<% form_tag (:action => ‘uploadXML’, :multipart => true) do %>
<%= file_field_tag ‘file’ %>


<%= submit_tag “Upload” %>
<% end %>

controller:
require ‘rexml/document’
include REXML

def uploadXML
xml_file = params[:file].read
elements = Document.new File.new(xml_file)

elements.root.each_element do |e|
  .......
end

end

I am not sure if you can directly read params[:file] as a file handle.
I
could be wrong.

if you debug your output by typing logger.info(#{xml_file}) might
provide
some hints.

On Tue, Jul 1, 2008 at 1:48 PM, loominator1970
[email protected]

On 1 Jul 2008, at 18:48, loominator1970 wrote:

thanks,
require ‘rexml/document’
include REXML

def uploadXML
xml_file = params[:file].read
elements = Document.new File.new(xml_file)

Just elements = Document.new params[:file]
should do the trick, or elements = Document.new xml_file would
probably work too.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs