为什么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
)。
为了和这个网络服务进行通信,我使用了SOAPpy这个库。下面是网络服务返回的一个示例结果(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 个回答
1
你发的那个响应示例(从网络服务请求返回的实际XML)里没有你想要的值!我觉得这就是SOAPpy无法把它返回给你的原因。
如果你想让你的代码在有值和没有值的情况下都表现一致,可以试试用dict
的get()
方法来获取这个值:
attribute_value = result.get("attribute", None)
这样你就可以检查结果是否为None(空值)。你也可以这样做:
if not "attribute" in result:
...handle case where there is no attribute value...