使用 SUDS 时添加 xsi:type 和信封命名空间 [英] Adding xsi:type and envelope namespace when using SUDS
问题描述
我需要与 SOAP 服务进行交互,但在这样做时遇到了很多麻烦;真的很感激这方面的任何指示.原来的错误信息是:
I need to interact with a SOAP service and am having a lot of trouble doing so; would really appreciate any pointers on this. The original error message was:
org.apache.axis2.databinding.ADBException: Any type element type has not been given
经过一番研究,发现这是SUDS和服务器之间的分歧,要如何处理
After some research, it turns out that this is a disagreement between SUDS and the server has to how deal with
type="xsd:anyType"
在相关元素上.
我已经确认使用了 SOAPUI,并根据建议可以通过以下步骤解决问题:
I've confirmed using SOAPUI and after advice that the problem can be fixed by taking these steps:
- 将 xsi:type="xsd:string" 添加到导致问题的每个元素
- 将 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 添加到 SOAP 信封
因此,SUDS 目前在何处执行此操作:
So, where SUDS currently does this:
<SOAP-ENV:Envelope ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Body>
<ns0:method>
<parameter>
<values>
<table>
<key>EMAIL_ADDRESS</key>
<value>example@example.org</value>
</table>
</values>
</parameter>
</ns0:method>
它应该产生这个:
<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Body>
<ns0:method>
...
<parameter>
<values>
<table>
<key xsi:type="xsd:string">EMAIL_ADDRESS</key>
<value xsi:type="xsd:string">example@example.org</value>
</table>
</values>
</parameter>
</ns0:method>
有没有正确的方法来做到这一点?我已经看到使用 ImportDoctor 或 MessagePlugins 的建议,但还没有真正理解如何达到预期的效果.
Is there a correct way to do this? I've seen suggestions of using ImportDoctor or MessagePlugins, but haven't really grokked how to achieve the desired effect.
推荐答案
我找到的解决方案是使用 MessagePlugin 在发送之前基本上手动修复 XML.我希望有更优雅的东西,但至少这是有效的:
The solution I found was to use a MessagePlugin to essentially manually fix up the XML just before sending. I'd hoped there was something more elegant, but at least this works:
class SoapFixer(MessagePlugin):
def marshalled(self, context):
# Alter the envelope so that the xsd namespace is allowed
context.envelope.nsprefixes['xsd'] = 'http://www.w3.org/2001/XMLSchema'
# Go through every node in the document and apply the fix function to patch up incompatible XML.
context.envelope.walk(self.fix_any_type_string)
def fix_any_type_string(self, element):
"""Used as a filter function with walk in order to fix errors.
If the element has a certain name, give it a xsi:type=xsd:string. Note that the nsprefix xsd must also
be added in to make this work."""
# Fix elements which have these names
fix_names = ['elementnametofix', 'anotherelementname']
if element.name in fix_names:
element.attributes.append(Attribute('xsi:type', 'xsd:string'))
这篇关于使用 SUDS 时添加 xsi:type 和信封命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!