2009-07-27 43 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

相關問題