1

I'm new to learning Rails and currently trying to complete a project for school that involves using of XML along with an MVC architecture. I'm using Rails as my MVC and I just want to know how I should approach adding in the XML and then rendering it to display that information.

I know that xml.builder allows me to build the XML using more DRY philosophy but I'm looking for a good tutorial that would explain the process of doing that and then display it in my browser. Any information is appreciated. Thanks in advance.

2
  • Can you clarify more, what you want to do exactly? you want to render data from DB and show it as XML or you want to read XML data and pass it to DB ? Commented Apr 22, 2018 at 22:00
  • I want to read XML data and then display it in my html Commented Apr 22, 2018 at 22:10

1 Answer 1

1

You can use from_xml to parse XML data to hash:

xml = <<-XML
  <?xml version="1.0" encoding="UTF-8"?>
    <hash>
      <foo type="integer">1</foo>
      <bar type="integer">2</bar>
    </hash>
XML

hash = Hash.from_xml(xml)
# => {"hash"=>{"foo"=>1, "bar"=>2}}

Reading from a local file:

# reading the file content into a variable  
xml_file = File.read("my_xml_file.xml")
hash = Hash.from_xml(xml_file)

Reference:
https://apidock.com/rails/v4.2.7/Hash/from_xml/class

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I need a bit more information. Like which file do I place that into, does it accept an external xml file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.