如何在 Savon 中使用带有 xsi:types 的对象 [英] How to use objects with xsi:types in Savon

查看:27
本文介绍了如何在 Savon 中使用带有 xsi:types 的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Savon 发出一些 SOAP 请求,但恐怕我需要在一定程度上超越基础.

I'm trying to use Savon to make some SOAP requests, but I'm afraid I need to go beyond the basics somewhat.

我需要发送以下内容:

<env:Body>
  <wsdl:methodName>
    <parameter xsi:type='ValueClass'>value</parameter>
  </wsdl:methodName>
</env:Body>

现在,如果我不必指定那个 xsi:type,那就很简单了:

Now, if I didn't have to specify that xsi:type, it would be a simple matter of:

client.method_name { |soap| soap.body = {:parameter => 'value'} }

问题是参数中的xsi:type;由于我使用的 Web 服务是围绕多态构建的,我需要明确指定参数的类型.有什么办法可以做到这一点(最好不必生成我自己的 XML?)我真的很想永远放弃 soap4r :)

The problem is the xsi:type in the parameter; due to the way the web service I'm using is built around polymorphism, I need to explicitly specify what type the parameter is. Is there any way I can do this (preferably without having to generate my own XML?) I'd really love to drop soap4r for good :)

谢谢!

推荐答案

在 Hash 中指定 XML 属性非常难看,但它是可能的:

Specifying XML attributes in a Hash is pretty ugly, but it's possible:

client.method_name do |soap|
  soap.body = {
    :parameter => 'value',
    :attributes! => { :parameter => { 'xsi:type' => ValueClass } }
  }
end

请查看:http://github.com/rubiii/savon/wiki/肥皂

在 Savon 支持 XML Schema Attributes 之前,我建议您使用 Builder
(Savon 附带)以生成您的 XML:

Until Savon supports XML Schema Attributes, I would suggest you to use Builder
(which comes with Savon) to generate your XML:

client.method_name do |soap|
  xml = Builder::XmlMarkup.new
  soap.body = xml.parameter "value", "xsi:type" => "ValueClass"
end

这篇关于如何在 Savon 中使用带有 xsi:types 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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