【问题标题】:Python Parsing XML with SOAP NamespacesPython 使用 SOAP 命名空间解析 XML
【发布时间】:2018-07-19 13:22:11
【问题描述】:

如何使用命名空间解析 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>

我需要获取键和值。 谢谢!

【问题讨论】:

    标签: python xml soap elementtree


    【解决方案1】:

    对于解析 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 2019-11-06
      • 1970-01-01
      • 2021-05-03
      • 2014-01-10
      • 2012-12-05
      • 2010-11-08
      相关资源
      最近更新 更多