我有一个返回以下类型的Web服务:
<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
”的价值?
答案 0 :(得分:1)
您发布的响应示例(从WS请求返回的实际XML)没有您要查找的值!我建议这就是为什么SOAPpy无法将它返回给你。
如果是在返回值的情况下使代码具有一致行为的情况,那么请尝试使用dict
的{{1}}方法获取值:< / p>
get()
然后你可以测试结果为none。您也可以这样做:
attribute_value = result.get("attribute", None)