1

コントローラーに、呼び出されたときに XML 応答を返す URL があります。たとえば、応答は次のようになります。

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

statusこれらの値を読み取り、コントローラーの変数に入れるにnameはどうすればよいですか。location

前もって感謝します。

4

4 に答える 4

1

これを試してみませんか?

response_json = Hash.from_xml(response).to_json
于 2013-10-30T20:04:27.863 に答える
1

Rails 5のアップデートはこちら

XML 応答を受信して​​いる場合、ヘッダーは「application/xml」になります。次を使用してデータにアクセスします。

#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"]
于 2017-10-16T15:13:40.110 に答える
0

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
于 2014-09-05T21:04:54.260 に答える