如何使用Python解析SOAP XML

时间:2018-05-07 10:32:26

标签: python xml parsing soap

我有一些SOAP响应保存在我想要解析的文件中,

示例文件的一部分:

public class RedisCloudConfig extends AbstractCloudConfig {
    @Bean
    public RedisConnectionFactory redisConnection() {
        return connectionFactory().redisConnectionFactory();
    }
}

我想获得(FullName& AssetInstanceId):

<?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>
    <ns0:GetList_Operation_0Response xmlns:ns0="urn:COMPANY:TEST:Assets" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ns0:getListValues>
            <ns0:Status>New</ns0:Status>
            <ns0:FormType>Support Group</ns0:FormType>
            <ns0:PersonRole>Supported by</ns0:PersonRole>
            <ns0:FullName>Data Centre</ns0:FullName>
            <ns0:PeopleGroupFormEntryID>SG0003</ns0:PeopleGroupFormEntryID>
            <ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWDS</ns0:PeopleGroupInstanceID>
            <ns0:AssetClassId>UPS</ns0:AssetClassId>
            <ns0:AssetInstanceId>ASDAWDDAWSDWADS66666</ns0:AssetInstanceId>
        </ns0:getListValues>
        <ns0:getListValues>
            <ns0:Status>New</ns0:Status>
            <ns0:FormType>Support Group</ns0:FormType>
            <ns0:PersonRole>Supported by</ns0:PersonRole>
            <ns0:FullName>Unix</ns0:FullName>
            <ns0:PeopleGroupFormEntryID>SG0004</ns0:PeopleGroupFormEntryID>
            <ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWQQ</ns0:PeopleGroupInstanceID>
            <ns0:AssetClassId>COMPUTERSYSTEM</ns0:AssetClassId>
            <ns0:AssetInstanceId>ASDAWDDAWSDWADS55555</ns0:AssetInstanceId>
        </ns0:getListValues>
    </ns0:GetList_Operation_0Response>
</soapenv:Body>

你能建议最好的方法吗?每当我尝试使用ElementTree时,我都会收到错误

Data Centre;ASDAWDDAWSDWADS66666
Unix;ASDAWDDAWSDWADS55555

可能是因为每行开头的"SyntaxError: expected path separator (:)" 附件

1 个答案:

答案 0 :(得分:0)

看起来你的xml坏了。您可以尝试使用正则表达式来获取所需的值

<强>演示:

a = """<?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><ns0:GetList_Operation_0Response xmlns:ns0="urn:COMPANY:TEST:Assets" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:getListValues>
    <ns0:Status>New</ns0:Status>
    <ns0:FormType>Support Group</ns0:FormType>
    <ns0:PersonRole>Supported by</ns0:PersonRole>
    <ns0:FullName>Data Centre</ns0:FullName>
    <ns0:PeopleGroupFormEntryID>SG0003</ns0:PeopleGroupFormEntryID>
    <ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWDS</ns0:PeopleGroupInstanceID>
    <ns0:AssetClassId>UPS</ns0:AssetClassId>
    <ns0:AssetInstanceId>ASDAWDDAWSDWADS66666</ns0:AssetInstanceId>
</ns0:getListValues>
<ns0:getListValues>
    <ns0:Status>New</ns0:Status>
    <ns0:FormType>Support Group</ns0:FormType>
    <ns0:PersonRole>Supported by</ns0:PersonRole>
    <ns0:FullName>Unix</ns0:FullName>
    <ns0:PeopleGroupFormEntryID>SG0004</ns0:PeopleGroupFormEntryID>
    <ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWQQ</ns0:PeopleGroupInstanceID>
    <ns0:AssetClassId>COMPUTERSYSTEM</ns0:AssetClassId>
    <ns0:AssetInstanceId>ASDAWDDAWSDWADS55555</ns0:AssetInstanceId>
</ns0:getListValues>"""

import re
FullName = re.findall("<ns0:FullName>(.*?)</ns0:FullName>", a)
AssetInstanceId = re.findall("<ns0:AssetInstanceId>(.*?)</ns0:AssetInstanceId>", a)
for i in zip(FullName, AssetInstanceId):
    print(i)

<强>输出:

'Data Centre', 'ASDAWDDAWSDWADS66666'
'Unix', 'ASDAWDDAWSDWADS55555'