【问题标题】:Adding xsi:type and envelope namespace when using SUDS使用 SUDS 时添加 xsi:type 和信封命名空间
【发布时间】:2012-06-10 14:23:36
【问题描述】:

我需要与 SOAP 服务进行交互,但在这样做时遇到了很多麻烦;非常感谢对此的任何指示。原来的错误信息是:

org.apache.axis2.databinding.ADBException: Any type element type has not been given

经过一番研究,原来这是SUDS和服务器对如何处理的分歧

type="xsd:anyType"

关于有问题的元素。

我已确认使用 SOAPUI,并在建议可以通过以下步骤解决问题后:

  1. 向每个导致问题的元素添加 xsi:type="xsd:string"
  2. 将 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>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 的建议,但还没有真正了解如何达到预期的效果。

【问题讨论】:

    标签: python xml soap wsdl suds


    【解决方案1】:

    我找到的解决方案是使用 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'))
    

    【讨论】:

    • 这对我有用,但是,我希望有人找到对实际框架本身的更好修复,这样我们就不必解决这个问题。
    • 英雄!我花了几个小时才弄明白这个
    • 您好,我对使用 SoapFixer 非常感兴趣。但是你如何实现插件,我尝试通过调用 client = Client(wsdl, plugins=['SoapFixer()']) 但这似乎不起作用
    • 已经很久了,但我记得应该可以。您收到错误消息吗?
    【解决方案2】:

    这既可悲又好笑,就像这个特定图书馆的很多事情一样,但这里是确切的答案:

    http://lists.fedoraproject.org/pipermail/suds/2011-September/001519.html

    从上面:

    soapenv = soapenv.encode('utf-8')
    plugins.message.sending(envelope=soapenv)
    

    变成:

    soapenv = soapenv.encode('utf-8')
    ctx = plugins.message.sending(envelope=soapenv)
    soapenv = ctx.envelope
    

    基本上,这是实现中的一个错误,您可以通过编辑运行插件的行来自己修补它以实际返回插件的结果,但我不知道修复此问题的 SUDS 的修补和更新版本然而(虽然我没有仔细寻找它)。

    【讨论】:

    • 这对我不起作用......但还没有投票。我本可以把事情搞砸的。还有其他人可以使用此解决方案吗?
    • 不知道告诉你什么 - 我在生产中使用它来完成我正在进行的项目,所以我很确定它可以工作。你有没有检查过soapenv、ctx和soapenv的值?您是否确认您的插件正在执行它们应该并返回结果
    • 我走了一个不同的方向 - 修复我的服务器端代码 :) stackoverflow.com/questions/13593950/… 我确实逐步完成了.. 我不太记得了,但我没有看到 soapenv IIRC 发生了太多事情。我可能完全错了。
    • 我只能说哇。希望我能够通过特定的 api 集成访问等式的服务器端,这种集成将我拖过这片特殊的泥泞。
    • 是的 - 在 magento 的情况下,这就是我正在做的事情,几乎所有东西都是'le sigh'。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    相关资源
    最近更新 更多