0

我正在使用萨文。动态生成多个 SOAP 正文 XML 标记的正确方法是什么?我正在考虑这种方式,这不是正确的方法。

item_id = "abc,def,xyz"

item_xml = ""
item_id.split(",").each do |e|
item_xml << 'ItemId' => "#{e}" #Sure this is a wrong way
end
begin

myclient = Savon::Client.new  do |wsdl, soap|
wsdl.document = "http://somthing.com/service?wsdl"
wsdl.soap_actions
end
result = myclient.request :v1, :update  do |soap|
soap.namespaces["xmlns:v1"] = "http://somthing.com/service?wsdl"
end


#This is how I do for manual single entry of ItemId
soap.body =  {
'Body' => {
            'ItemList' => {
'ItemId' => "abc123"
            }
      }
}

#Want to generate soap body with multiple ItemId
soap.body =  {
'Body' => {
            'ItemList' => {
item_xml 

#shall be equivalent as this 
#'ItemId' => "abc",
#'ItemId' => "def",
#'ItemId' => "xyz"

            }
      }
}

编辑: 如何根据元素的数量创建一个标签数组item_id

item_id = "abc, def, xyz"
n = item_id.split(,).length

    #shall be equivalent as this 
    #ItemList shall be of n times
soap.body =  {
    'Body' => {
                'ItemList' => {  
    'ItemId' => "abc"
                }
                'ItemList' => {  
    'ItemId' => "def"
                }
                'ItemList' => {  
    'ItemId' => "xyz"
                }
          }
    }
4

1 回答 1

0

使用 Savon 创建列表就像为特定键传递值数组一样简单:

require 'savon'

item_id = 'abc,def,xyz'

client = Savon::Client.new do |wsdl, soap|
  wsdl.endpoint = 'http://example.com'
  wsdl.namespace = 'http://example.com'
end

response = client.request :v1, :update  do |soap|
  soap.body =  {
    'ItemList' => {
      'ItemId' => item_id.split(',')
    }
  }
end
于 2012-05-07T08:56:01.057 回答