我需要与 SOAP 服务交互,但这样做遇到了很多麻烦;非常感谢对此的任何指示。原来的错误信息是:
org.apache.axis2.databinding.ADBException: Any type element type has not been given
经过一番研究,发现这是 SUDS 和服务器之间的分歧,如何处理
type="xsd:anyType"
关于有问题的元素。
我已确认使用 SOAPUI,并在建议后认为可以通过以下步骤解决该问题:
- 将 xsi:type="xsd:string" 添加到每个元素会导致问题
- 将 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 添加到 SOAP 信封
因此,SUDS 目前执行此操作的地方:
<SOAP-ENV:Envelope ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Body>
<ns0:method>
<parameter>
<values>
<table>
<key>EMAIL_ADDRESS</key>
<value>[email protected]</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">[email protected]</value>
</table>
</values>
</parameter>
</ns0:method>
有正确的方法吗?我已经看到使用 ImportDoctor 或 MessagePlugins 的建议,但还没有真正理解如何达到所需的效果。
我找到的解决方案是使用 MessagePlugin 在发送之前手动修复 XML。我希望有更优雅的东西,但至少这是有效的:
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'))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)