Python使用SOAP名称空间解析XML
如何使用名称空间分析python XML输出:Python使用SOAP名称空间解析XML,python,xml,soap,elementtree,Python,Xml,Soap,Elementtree,如何使用名称空间分析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">
<?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>
在服务器www.google.com上测试路径分隔符www.google.com:443
团体
我需要得到关键和价值。
谢谢 对于解析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