本文介绍了Savon 将 XML 作为字符串返回,而不是散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Savon 解析 SOAP 响应.响应是 XML,但作为一个长字符串返回.如果我使用 #to_hash 整个 XML 对象仍然是一个字符串,现在存储在

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.

我的代码看起来像

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]

我错过了哪一步才能从响应中获取有用信息?注意:不幸的是,我无法发布 XML 响应,因为它包含的信息,但它看起来像存储为字符串的整个 XML 文档.它的类是 Nori::StringWithAttributes

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

推荐答案

我能够得到想要的结果,但使用 本文档.这似乎是一个不太理想的方法,但我意识到最后一个元素是一个哈希数组.所以它是散列,散列,带有散列数组.无论如何,这对我有用.关于如何使这个不那么丑陋和笨重的建议将不胜感激.

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] }

这篇关于Savon 将 XML 作为字符串返回,而不是散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:48