[Rails] How to read the XML file uploaded from the screen with Hash type

This is an implementation example when you want to read the XML file uploaded from the screen in Hash type in the application using Rails (5 series).

XML file to upload

This time I use the following XML file for verification

<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item>AAA</item>
  <item>BBB</item>
  <item>CCC</item>
</items>

Description of html.erb

Place the file and button tag appropriately. (You can choose your design style!)

<%= form_tag xxx_path, multipart: true do %>
  <label>Read XML file</label>
  <div class="row">
    <div class="col-sm-2">
      <%= file_field_tag :file, class: 'btn btn-primary' %>
    </div>
  </div>
  <div class="row">
    <div class="col-sm-4">
      <%= submit_tag 'Send', class: 'btn btn-primary' %>
    </div>
  </div>
<% end %>

Description of Controller.rb

xml = REXML::Document.new(File.new(params[:file].path).read)
xml_h = Hash.from_xml(xml.to_s)

As a result of the above, you can get the contents of the file in Hash format as shown below.

{"items"=>{"item"=>["AAA", "BBB", "CCC"]}}

Recommended Posts

[Rails] How to read the XML file uploaded from the screen with Hash type
How to convert param to hash with Rails controller (updated from time to time)
How to compare only the time with Rails (from what time to what time, something like)
[Java] How to extract the file name from the path
How to read rails routes
How to transition from the [Swift5] app to the iPhone settings screen
How to write a migration from Rails datetime type to date type
[With back tricks] How to introduce React to the simplest Rails
How to change the file name with Xcode (Refactor Rename)
How to read a library from a JAR file with VS Code << How to not use Maven / Gradle >>
[Rails] How to prevent screen transition
How to get along with Rails
[Rails] How to introduce kaminari with Slim and change the design
How to open a script file from Ubuntu with VS code
[Java] How to convert from String to Path type and get the path
[Rails 5] How to display the password change screen when using devise
[Problem solving] SAXReader: How to read an xml file with an external DTD file specified in an offline environment
How to decorate the radio button of rails6 form_with (helper) with CSS
[Java] How to use the File class
How to delete the wrong migration file
[Rails] How to convert from erb to haml
How to run a GIF file from the Linux command line (Ubuntu)
[Rails] How to use rails console with docker
How to delete the migration file NO FILE
[Rails] How to use the map method
How to achieve file download with Feign
[Rails] How to get the user information currently logged in with devise
How to pass the value to another screen
[Rails] How to create a table, add a column, and change the column type
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
How to achieve file upload with Feign
How to build Rails 6 environment with Docker
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
I want to be able to read a file using refile with administrate [rails6]
[Rails] How to register multiple records in the intermediate table with many-to-many association
[Rails] How to operate the helper method used in the main application with Administrate
How to get the date from the JavaScript Date type that C # developers are addicted to
[Rails] How to decide the destination by "rails routes"
[Rails] I tried to raise the Rails version from 5.0 to 5.2
How to link Rails6 Vue (from environment construction)
How to dump from database (DB) to seeds file
[Swift] How to link the app with Firebase
I want to play with Firestore from Rails
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
Rails "How to delete NO FILE migration files"
[Spring Boot] How to refer to the property file
Try to summarize the common layout with rails
How to check Rails commands in the terminal
If you use SQLite with VSCode, use the extension (how to see the binary file of sqlite3)
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
How to save a file with the specified extension under the directory specified in Java to the list
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
How to change the action with multiple submit buttons
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
How to return to the previous screen by Swipe operation
How to jump from Eclipse Java to a SQL file