使用 SAVON response.to_hash 转换方法解析 SOAP 响应 [英] Parsing SOAP response using SAVON response.to_hash conversion method

查看:66
本文介绍了使用 SAVON response.to_hash 转换方法解析 SOAP 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析 SOAP 响应时遇到问题.

I am having trouble parsing a SOAP response.

这是我的代码:

require 'rubygems'
require 'savon'

client = Savon::Client.new "http://1.2.3.4/xyz/abcd/fsds.wsdl"

res = client.query_unpaid_assesments do |soap,wsse|
  soap.namespaces["xmlns:SOAP-ENV"] = "http://schemas.xmlsoap.org/soap/envelope/"
  soap.namespaces["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
  soap.namespaces["xmlns:xsd"] = "http://www.w3.org/2001/XMLSchema"

  wsse.username="xyz"
  wsse.password="123"

  soap.body = {:orderNumber => 111222333 }
end

response = Savon::Response#to_hash
hres = response.to_hash 
all_data = hres[:response][:asses_data][:date][:amount][:assesReference][:year][:cusOffCode][:serie][:number][:date][:time]

这是我遇到的错误:

未定义方法 to_hash for Savon::Response:Class (NoMethodError)

undefined method to_hash for Savon::Response:Class (NoMethodError)

"res" 给了我想要散列的 xml 响应.

"res" is giving me xml response that I would like to have in hash.

我阅读了以前的相关问题,他们建议使用 response.to_hash ,我这样做了并抛出了上面指定的错误.我怎样才能摆脱这个错误并将我的响应变成哈希.

I read previous related questions and they recommending to use response.to_hash , which I did and is throwing the error specified above. How can i get rid of this error and have my response into hash.

感谢您的帮助

我忘记发布我想解析的 xml 响应的正文:

I forgot to post the body of the xml response that I would like to parse:

<soapenv:Body>
<response>
<ns203:assesData xmlns:ns203="http://asdfsd.sdfsd.zbc.org">
<ns203:date>2010-09-01</ns203:date>
<ns203:amount>34400</ns203:amount>
<ns203:asesReference>
    <ns203:year>2010</ns203:year>
    <ns203:cusOffCode>098</ns203:customsOfficeCode>
    <ns203:serie>F</ns203:serie>
    <ns203:number>524332</ns203:number>
    <ns203:date>2010-11-11</ns203:date>
    <ns203:time>10:11:103</ns203:time>
</ns203:assesReference>
</ns203:assesData>
</response>
</soapenv:Body>

推荐答案

我相信您需要尝试 #to_hash res 本身,即返回的 Savon::Response 对象,而不是 Savon::响应类.

I believe you need to be trying to #to_hash res itself, the returned Savon::Response object, instead of the Savon::Response class.

所以 hres = res.to_hash 应该可以工作.

So hres = res.to_hash should work.

我发现的一个例子(在这里的最后:http://blog.nofail.de/2010/01/savon-vs-handsoap-calling-a-service/) 应该给你这个想法.

An example I found (at the end of here: http://blog.nofail.de/2010/01/savon-vs-handsoap-calling-a-service/) should give you the idea.

class SavonBankCode
  def self.zip_code(bank_code)
    client = Savon::Client.new Shootout.endpoints[:bank_code][:uri]
    response = client.get_bank { |soap| soap.body = { "wsdl:blz" => bank_code } }
    response.to_hash[:get_bank_response][:details][:plz]
  end
end

另一种方法是使用 Nokogiri 或类似方法解析结果,这意味着您可以执行以下操作:

An alternative would be to parse the result with Nokogiri or similar, meaning you could do something like this:

require 'nokogiri'
response = res.xpath("//ns203:assesData", "ns203" => "http://asdfsd.sdfsd.zbc.org")
date = response.xpath("ns203:date", "ns203" => "http://asdfsd.sdfsd.zbc.org")
amount = response.xpath("ns203:amount", "ns203" => "http://asdfsd.sdfsd.zbc.org")
number = response.xpath("ns203:asesReference/ns203:number", "ns203" => "http://asdfsd.sdfsd.zbc.org")

等等.等等.当然像罪一样丑陋,但嘿,这是一种(未经测试或改进的)替代品;)

etc. etc. Ugly as sin of course, but hey it is an (untested or refined) alternative ;)

祝你好运!

这篇关于使用 SAVON response.to_hash 转换方法解析 SOAP 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆