使用 Nokogiri 从与 Savon 的 SOAP 调用中读取响应 [英] Read response with Nokogiri from a SOAP call with Savon

查看:76
本文介绍了使用 Nokogiri 从与 Savon 的 SOAP 调用中读取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和Savon打了个肥皂电话.这工作正常,并给以下回复:

I have make a soap-call with Savon. This works fine and give the following response:

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http:// 
schemas.xmlsoap.org/soap/envelope/"> 
  <soap:Body> 
    <GetTop10Response xmlns="http://www.kirupafx.com"> 
      <GetTop10Result> 
        <string>string</string> 
        <string>string</string> 
      </GetTop10Result> 
    </GetTop10Response> 
  </soap:Body> 
</soap:Envelope> 

现在我想从响应中取出所有字符串元素.但我无法让它工作.

Now I want to take all of the string elements out of the response. But I can't get it to work.

def query(params=nil)

    client = Savon::Client.new do
      wsdl.document = "http://www.kirupafx.com/WebService/TopMovies.asmx?wsdl"
    end

    response = client.request :get_top10

    if response.success?
      xml = Nokogiri::XML(response.to_xml)
      print "Until here oké!"
      xml.search('//GetTop10Result').each do |result|
        print "How are you Ruby?"
        @result[result.at('string').inner_text] = result.at('string').inner_text
      end
    else
      raise "Error!"
end

但他从不打印我漂亮的你好,Ruby?"有人可以帮忙吗我?我做错了什么?

But he never prints my beautiful "How are you Ruby?" Can somebody help me? What I'm doing wrong?

推荐答案

您可以这样做,但这不是处理此类问题的最佳方法!您可能有使用 Nokogiri 和 XML 的经验,但像这样使用 .to_hash 更容易.

You could to this but this isnt the best way to deal with problems like this! You might have experience using Nokogiri and XML but its easier to use .to_hash like this.

def query
    client = Savon::Client.new do
          wsdl.document = "http://www.kirupafx.com/WebService/TopMovies.asmx?wsdl"
    end
    response = client.request(:get_top10)
    response.to_hash[:get_top10_response][:get_top10_result] if response.success?
    false
end

这篇关于使用 Nokogiri 从与 Savon 的 SOAP 调用中读取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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