Savon returning XML as string, not hash

自古美人都是妖i 提交于 2019-12-24 08:20:45

问题


I am trying to parse a SOAP response using Savon. The response is XML but is being returned as one long string. If I use #to_hash the entire XML object is still a string, now stored in

hash[:response][:return]

which means it is still a huge unusable mess.

My code looks like

response = soapClient.request(:get_sites_user_can_access) do
  soap.body = { :sessionid  => session[:login_response][:login_return],
                :eid        => user }
end

rep = response.to_hash

pp rep[:get_sites_user_can_access_response][:get_sites_user_can_access_return]

What step am I missing to get useful information out of the response? Note: Unfortunately I can't post the XML response because of the info it contains, but it looks like an entire XML document stored as a string. It's class is Nori::StringWithAttributes


回答1:


I was able to get the desired results but parsing the Nori string(?) using this documentation. This seems like a less than ideal method, but I realized the last element is an array of hashes. So it's hash, of hashes, with an array of hashes. Anyway, here is what worked for me. Advice on how to make this less ugly and clunky would be appreciated.

response = soapClient.request(:get_sites_user_can_access) do
  soap.body = { :sessionid  => session[:login_response][:login_return],
                :eid        => user }
end

rep = response.to_hash[:get_sites_user_can_access_response][:get_sites_user_can_access_return]

hrep = Nori.parse(rep)

hrep[:list][:item].each { |item| pp item[:site_id] }


来源:https://stackoverflow.com/questions/10079148/savon-returning-xml-as-string-not-hash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!