【发布时间】:2021-03-03 06:18:19
【问题描述】:
这是我的 XML:
x = """<?xml version="1.0" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Fault occurred</faultstring>
<faultactor>http://xxxxx/PrivateOfficeDataService.asmx</faultactor>
<detail>
<details xmlns="http://privateoffice.manzana.ru/">
<code>100261</code>
<description>access denied</description>
</details>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>"""
from lxml import etree
root = etree.fromstring(x)
现在,我想找到根的一个子节点。
这不起作用:
print(root.find('Body'))
# None
这行得通:
print(root.find('{http://schemas.xmlsoap.org/soap/envelope/}Body'))
# <Element {http://schemas.xmlsoap.org/soap/envelope/}Body at 0x10e6a1c40>
但这太麻烦了,所以我试试这个(也不起作用):
print(root.find('Body', namespaces={'soap': 'http://schemas.xmlsoap.org/soap/envelope/'}))
# None
我如何使用这个namespaces arg 以便它工作?
【问题讨论】: