我使用Python脚本从SOAP服务接收XML响应,我想从XML响应中提取特定的值。我正在尝试使用“getting”库,但始终得到以下错误:
AttributeError:“无”没有属性“信封”
下面是我的代码示例。我试图从下面提取RequestType值
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body>
<Response>\n
<RequestType>test</RequestType>
</Response>
</soap:Body>
</soap:Envelope>解缠的样本使用
parsed_xml = untangle.parse(xml)
print(parsed_xml.Envelope.Response.RequestType.cdata)我也试过parsed_xml.Envelope.Body.Response.RequestType.cdata
发布于 2021-10-29 02:34:52
这将解决您的问题,假设您想要提取‘测试’。顺便说一下,我认为您的响应不应该有'soap:Header/':
import xmltodict
stack_d = xmltodict.parse(response.content)
stack_d['soap:Envelope']['soap:Body']['Response']['RequestType']发布于 2021-05-17 07:33:12
我认为您会发现xml.etree库在这个上下文中更有用。
import requests
from xml.etree import ElementTree然后,我们需要为SOAP响应定义名称空间。
namespaces = {
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'a': 'http://www.etis.fskab.se/v1.0/ETISws',
}
dom = Element.tree.fromstring(response.context)然后简单的找到所有的王国
names = dom.findall('./soap:Body',namespaces)https://stackoverflow.com/questions/67565291
复制相似问题