如何用 Python 解析 SOAP XML? [英] How to parse SOAP XML with Python?
问题描述
目标:获取
标签内的值并打印出来.下面是简化的 XML.
Goal:
Get the values inside <Name>
tags and print them out. Simplified XML below.
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetStartEndPointResponse xmlns="http://www.etis.fskab.se/v1.0/ETISws">
<GetStartEndPointResult>
<Code>0</Code>
<Message />
<StartPoints>
<Point>
<Id>545</Id>
<Name>Get Me</Name>
<Type>sometype</Type>
<X>333</X>
<Y>222</Y>
</Point>
<Point>
<Id>634</Id>
<Name>Get me too</Name>
<Type>sometype</Type>
<X>555</X>
<Y>777</Y>
</Point>
</StartPoints>
</GetStartEndPointResult>
</GetStartEndPointResponse>
</soap:Body>
</soap:Envelope>
尝试:
import requests
from xml.etree import ElementTree
response = requests.get('http://www.labs.skanetrafiken.se/v2.2/querystation.asp?inpPointfr=yst')
# XML parsing here
dom = ElementTree.fromstring(response.text)
names = dom.findall('*/Name')
for name in names:
print(name.text)
我读过其他人推荐 zeep
解析soap xml,但我发现很难理解.
I have read other people recommending zeep
to parse soap xml but I found it hard to get my head around.
推荐答案
这里的问题是处理 XML 命名空间:
The issue here is dealing with the XML namespaces:
import requests
from xml.etree import ElementTree
response = requests.get('http://www.labs.skanetrafiken.se/v2.2/querystation.asp?inpPointfr=yst')
# define namespace mappings to use as shorthand below
namespaces = {
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
'a': 'http://www.etis.fskab.se/v1.0/ETISws',
}
dom = ElementTree.fromstring(response.content)
# reference the namespace mappings here by `<name>:`
names = dom.findall(
'./soap:Body'
'/a:GetStartEndPointResponse'
'/a:GetStartEndPointResult'
'/a:StartPoints'
'/a:Point'
'/a:Name',
namespaces,
)
for name in names:
print(name.text)
命名空间来自 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
和 xmlns="http://www.etis.分别位于
属性.Envelope
和 GetStartEndPointResponse
节点上的 fskab.se/v1.0/ETISws"
The namespaces come from the xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
and xmlns="http://www.etis.fskab.se/v1.0/ETISws"
attributes on the Envelope
and GetStartEndPointResponse
nodes respectively.
请记住,即使名称空间未在子标签上明确指定为 <namespace:tag>
,父节点的所有子节点也会继承名称空间.
Keep in mind, a namespace is inherited by all children nodes of a parent even if the namespace isn't explicitly specified on the child's tag as <namespace:tag>
.
注意:我必须使用 response.content
而不是 response.body
.
Note: I had to use response.content
rather than response.body
.
这篇关于如何用 Python 解析 SOAP XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!