【问题标题】:Why Python omits attribute in the SOAP message?为什么 Python 在 SOAP 消息中省略了属性?
【发布时间】:2009-07-27 14:10:55
【问题描述】:

我有一个返回以下类型的网络服务:

<xsd:complexType name="TaggerResponse">
    <xsd:sequence>
        <xsd:element name="msg" type="xsd:string"></xsd:element>
    </xsd:sequence>
    <xsd:attribute name="status" type="tns:Status"></xsd:attribute>
</xsd:complexType>

该类型包含一个元素 (msg) 和一个属性 (status)。

为了与 Web 服务通信,我使用 SOAPpy 库。以下是 Web 服务返回的示例结果(SOAP 消息):

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:TagResponse>
         <parameters status="2">
            <msg>text</msg>
         </parameters>
      </SOAP-ENV:TagResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Python 将此消息解析为:

<SOAPpy.Types.structType parameters at 157796908>: {'msg': 'text'}

如您所见,该属性已丢失。我应该怎么做才能获得“status”的值?

【问题讨论】:

    标签: python web-services soap wsdl soappy


    【解决方案1】:

    您发布的响应示例(从 WS 请求返回的实际 XML)没有您要查找的值!我建议这就是 SOAPpy 无法将其返回给您的原因。

    如果您的代码在返回值的情况下具有一致的行为,而在返回值不返回的情况下,请尝试使用dictget() 方法获取值:

    attribute_value = result.get("attribute", None)
    

    然后您可以测试结果是否为无。你也可以这样做:

    if not "attribute" in result:
        ...handle case where there is no attribute value...
    

    【讨论】:

    • 我想要消息中的“status”值。 get 方法不适用于结果:structType instance has no attribute 'get'。但是,您的回答给了我一个线索,我发现使用 r._getAttr("status") 是我正在寻找的值。但是,..._getAttr 是一个内部函数,我可以使用它还是有更合适的方法来获取值?
    • czuk:如果类继承自对象,您可以使用 getattr() 内置函数。例如 getattr(result, "status", None) 将返回结果中的状态值,如果未设置,则返回 None。
    • 函数getattr也不存在。我想知道这是否可能是 WSDL 声明的问题。其中一个验证器说缺少命名空间属性。我正在努力。
    • 尝试使用dir() 函数自省结果实例:这将告诉您它具有哪些属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2017-11-01
    • 1970-01-01
    • 2011-04-10
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多