2009-10-31 134 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