2009-10-31 47 views
1

所以我需要從我的rails應用程序訪問此服務。我使用soap4r來讀取WSDL並動態生成訪問服務的方法。使用soap4r訪問SOAP服務無法訪問返回對象的內容

從我讀過的,我應該能夠鏈接方法訪問嵌套的XML節點,但我不能讓它工作。我嘗試使用wsdl2ruby命令並讀取生成的代碼。從我所知道的,soap庫不會生成這些訪問器方法。我對ruby很新,所以我不知道我是否錯過了一些東西?

我知道當我檢查元素時,我可以看到我想要的數據。我無法做到。

舉例來說,如果我使用下面的代碼:

require "soap/wsdlDriver" 
wsdl = "http://frontdoor.ctn5.org/CablecastWS/CablecastWS.asmx?WSDL" 
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver 
response = driver.getChannels('nill') 
puts response.inspect 

我得到以下輸出:

ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}binding 
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}operation 
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}body 
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}address 
#<SOAP::Mapping::Object:0x80b96394 {http://www.trms.com/CablecastWS/}GetChannelsResult=#<SOAP::Mapping::Object:0x80b96178 {http://www.trms.com/CablecastWS/}Channel=[#<SOAP::Mapping::Object:0x80b95f5c {http://www.trms.com/CablecastWS/}ChannelID="1" {http://www.trms.com/CablecastWS/}Name="CTN 5">, #<SOAP::Mapping::Object:0x80b9519c {http://www.trms.com/CablecastWS/}ChannelID="2" {http://www.trms.com/CablecastWS/}Name="PPAC 2">, #<SOAP::Mapping::Object:0x80b94620 {http://www.trms.com/CablecastWS/}ChannelID="14" {http://www.trms.com/CablecastWS/}Name="Test Channel">]>> 

所以數據肯定是存在的!

下面是wsdl2ruby生成的代碼上面所使用的方法:

# {http://www.trms.com/CablecastWS/}GetChannels 
class GetChannels 
    def initialize 
    end 
end 

# {http://www.trms.com/CablecastWS/}GetChannelsResponse 
# getChannelsResult - ArrayOfChannel 
class GetChannelsResponse 
    attr_accessor :getChannelsResult 

    def initialize(getChannelsResult = nil) 
    @getChannelsResult = getChannelsResult 
    end 
end 

對不起,長的帖子,我想更多的信息越有可能有人能指出我在正確的方向。

感謝

射線

回答

4

回答

require "soap/wsdlDriver" 
wsdl = "http://frontdoor.ctn5.org/CablecastWS/CablecastWS.asmx?WSDL" 
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver 
response = driver.getChannels('nill') 

for item in response.getChannelsResult.channel 
    puts item.name 
    puts item.channelID 
end 

我如何得到的答案

您可以通過

找出應對方法10
response.methods 

這會給你一個難以排序的很長的方法列表,所以我想減去通用方法。 Ruby可以讓你減去數組。

response.methods - Object.new.methods 

使用這種技術,我找到了getChannelsResult方法來進行響應。我重複了這個過程

resonse.getChannelsResult.methods - Object.new.methods 

我找到了它的結果通道方法。再次!

response.getChannelsResult.channel.methods - Object.new.methods 

這返回了一堆方法,包括:sort,min,max等。所以我猜數組。一個簡單的確認是爲了

response.getChannelsResult.channel.class 

果然,它返回數組。爲了讓生活簡單,我只是與陣列的第一項工作,以獲取其方法

response.getChannelsResult.channel.first.methods - Object.new.methods 

Whoalla,我發現了另外兩種方法,「名」和「的channelID」

+0

謝謝!我實際上已經發現我昨晚晚些時候錯過了頻道方法,但是你的解釋對於用紅寶石計算出來是非常有用的。再次感謝。 – raytiley 2009-10-31 19:23:29