2013-10-30 2 views
1

내 컨트롤러에 XML 응답을 반환하는 URL이 있습니다. 예를 들어, 응답은 다음과 같습니다.레일스에서 ​​XML 응답 읽기

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

어떻게이 값 status, namelocation 변수에 내 컨트롤러에서하고 사용을 읽을 수 있습니다.

미리 감사드립니다.

+3

[this] (http://stackoverflow.com/questions/11139709/converting-from-xml-name-values-into-simple-hash) – Christian

답변

0

respose.body의 값이;

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

그렇다면이 부분은 훌륭하다고 생각합니다.

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

;

result.status == "YES" 

response_json = Hash.from_xml(response).to_json 
1

,

머리글이 될 것입니다 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"] 
0

당신은 그래서를 heres 레일 (5)에 대한 업데이 트를 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 
0

을 사용할 수 있습니다 당신이 시도 할 수