2009-07-27 71 views
0

我有一个返回以下类型的web服务:为什么Python在SOAP消息中省略了属性?

<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”的价值?

回答

1

您发布的响应示例(从WS请求返回的实际XML)没有您正在查找的值!我建议这就是为什么SOAPpy不能将它返回给你。

如果它使你的代码的情况下,在案件的一致行为,其中返回值,当它不是,则尝试使用dictget()方法来获取值:

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

然后你可以测试没有结果。你也可以这样做:

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

我想要在消息中的“状态”的值。 `get`方法对结果不起作用:`structType实例没有'get'`的属性。但是,您的回答给了我一个线索,我发现使用`r._getAttr(“status”)`我正在寻找的值。但是,`_getAttr`是一个内部函数,我可以使用它还是有更合适的方法来获取值? – czuk 2009-07-27 15:30:40

相关问题