[英]Python Parsing XML with SOAP Namespaces
如何使用命名空間解析python XML輸出:
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:searchResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://configuration.api.sitescope.mercury.com">
<searchReturn href="#id0"/>
</ns1:searchResponse>
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Map" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://xml.apache.org/xml-soap">
<item>
<key xsi:type="soapenc:string">test_sis_path_delimiter_www.google.com:443 on server www.google.com</key>
<value xsi:type="soapenc:string">Group</value>
</item>
</multiRef>
</soapenv:Body>
我需要獲得關鍵和價值。 謝謝!
為了解析XML,可以使用BeautifulSoup
。 這里的例子:
data = """<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:searchResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://configuration.api.sitescope.mercury.com">
<searchReturn href="#id0"/>
</ns1:searchResponse>
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Map" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://xml.apache.org/xml-soap">
<item>
<key xsi:type="soapenc:string">test_sis_path_delimiter_www.google.com:443 on server www.google.com</key>
<value xsi:type="soapenc:string">Group</value>
</item>
</multiRef>
</soapenv:Body>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'xml')
print(soup.key.text)
print(soup.value.text)
輸出:
test_sis_path_delimiter_www.google.com:443 on server www.google.com
Group
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.