1

I have a URL in my controller that when called return an XML response. Let say, the response looks like this;

<?xml version="1.0" encoding="UTF-8"?>
<AutoCreate>
<Response>
    <Status>YES</Status>
    <name>JOSEPH</name>
    <location>HOME</location>
</Response>
</AutoCreate>

How can i read these values status, name and location into variables in my controller and use them.

Thank you in advance.

acacia
  • 1,375
  • 1
  • 14
  • 40
  • 3
    Take a look at [this](http://stackoverflow.com/questions/11139709/converting-from-xml-name-values-into-simple-hash) – Christian Oct 30 '13 at 19:31

4 Answers4

1

can you try this,

response_json = Hash.from_xml(response).to_json
1

So heres an update for Rails 5,

If you are receiving an XML response the header will be 'application/xml' You access the data using

#read the response and create a Hash from the XML
response = Hash.from_xml(request.body.read)

#read value from the Hash
status = response["AutoCreate"]["Response"]["Status"]
Ndeto
  • 315
  • 6
  • 17
0

If the value of respose.body is;

<?xml version="1.0" encoding="UTF-8"?>
<AutoCreate>
<Response>
    <Status>YES </Status>
    <name> JOSEPH </name>
    <location> HOME </location>
</Response>
</AutoCreate>

Then i think this should be fine.

require ‘active_support’ 
result = Hash.from_xml(response.body)

then;

result.status  == "YES"

Would this work?

acacia
  • 1,375
  • 1
  • 14
  • 40
0

You can use https://github.com/alsemyonov/xommelier

feed = Xommelier::Atom::Feed.parse(open('spec/fixtures/feed.atom.xml'))
puts feed.id, feed.title, feed.updated

feed.entries do |entry|
  puts feed.id, feed.title, feed.published, feed.updated
  puts feed.content || feed.summary
end