使用Python etree返回空字典来解析soap响应

时间:2018-10-28 08:06:25

标签: python soap

我试图从下面的xml / soap响应中解析出sessionId字段,如下所示,但是所有子属性字典都是空的:

#!/usr/bin/python

import requests
from xml.etree import ElementTree
from xml.dom import minidom

url="http://172.16.46.85:8080/CAI3G1.2/services/CAI3G1.2"

headers = {'content-type': 'text/xml'}

body = """<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"             xmlns:cai3="http://schemas.ericsson.com/cai3g1.2/">
    <soapenv:Body>
            <cai3:Login>
                    <cai3:userId>admin</cai3:userId>
                    <cai3:pwd>admin</cai3:pwd>
            </cai3:Login>
    </soapenv:Body>
</soapenv:Envelope>
"""

response = requests.post(url,data=body,headers=headers)

print 'content : ' + str(response.content)
print '##################################################'
print 'text : ' + str(response.text)
print '##################################################'

root = ElementTree.fromstring(response.content)

for child in root.iter('*'):
    print 'child tag : ' + child.tag
    print 'child attrib : ' + str(child.attrib)

print '##################################################'

for child in root:
    print child.tag, child.attrib

print '##################################################'

以下是执行结果,我没有使用re寻找任何解决方案,我知道可以使用re库完成,我想知道如何使用etree(如果可以完成)来实现:

[root@web soap_python]# ./getSessionID_PG.py
content : <?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:LoginResponse xmlns:ns2="http://schemas.ericsson.com/cai3g1.2/"><ns2:sessionId>c70f2a0f7e7d4c5084bea857715e2d10</ns2:sessionId><ns2:baseSequenceId>306324732</ns2:baseSequenceId></ns2:LoginResponse></S:Body></S:Envelope>
##################################################
text : <?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:LoginResponse xmlns:ns2="http://schemas.ericsson.com/cai3g1.2/"><ns2:sessionId>c70f2a0f7e7d4c5084bea857715e2d10</ns2:sessionId><ns2:baseSequenceId>306324732</ns2:baseSequenceId></ns2:LoginResponse></S:Body>    </S:Envelope>
##################################################
child tag : {http://schemas.xmlsoap.org/soap/envelope/}Envelope
child attrib : {}
child tag : {http://schemas.xmlsoap.org/soap/envelope/}Body
child attrib : {}
child tag : {http://schemas.ericsson.com/cai3g1.2/}LoginResponse
child attrib : {}
child tag : {http://schemas.ericsson.com/cai3g1.2/}sessionId
child attrib : {}
child tag : {http://schemas.ericsson.com/cai3g1.2/}baseSequenceId
child attrib : {}
##################################################
{http://schemas.xmlsoap.org/soap/envelope/}Body {}
##################################################
[root@web soap_python]

1 个答案:

答案 0 :(得分:0)

xmlns之外,所有标签都没有属性,ElemenTree似乎排除了这些属性,因为它是一些“众所周知”的信息。如果您需要sessionId标签,则应该已经知道它是http://schemas.ericsson.com/cai3g1.2/名称空间的一部分。

通过Element.find(),您可以使用XPath表达式搜索特定的标签。例如:

from xml.etree import ElementTree
s = '''<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:LoginResponse xmlns:ns2="http://schemas.ericsson.com/cai3g1.2/"><ns2:sessionId>c70f2a0f7e7d4c5084bea857715e2d10</ns2:sessionId><ns2:baseSequenceId>306324732</ns2:baseSequenceId></ns2:LoginResponse></S:Body>    </S:Envelope>'''
root = ElementTree.fromstring(s)
ns = {'cai3g1.2': 'http://schemas.ericsson.com/cai3g1.2/'}
root.find('.//cai3g1.2:sessionId', ns).text
'c70f2a0f7e7d4c5084bea857715e2d10'
root.find('.//cai3g1.2:baseSequenceId', ns).text
'306324732'