Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ruby on rails - Read response with Nokogiri from a SOAP call with 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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...