[英]Extract XML data from SOAP Response in Python
I am using a Python script to receive an XML response from a SOAP web service, and I'd like to extract specific values from the XML response. 我正在嘗試使用“解開”庫,但不斷收到以下錯誤:
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>
untangle 的示例使用
parsed_xml = untangle.parse(xml)
print(parsed_xml.Envelope.Response.RequestType.cdata)
我也試過parsed_xml.Envelope.Body.Response.RequestType.cdata
假設您要提取“測試”,這將解決您的問題。 順便說一句,我認為您的回復不應包含“soap:Header/”:
import xmltodict
stack_d = xmltodict.parse(response.content)
stack_d['soap:Envelope']['soap:Body']['Response']['RequestType']
我想你會發現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)
然后簡單地找到所有的 DOM
names = dom.findall('./soap:Body',namespaces)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.