我尝试过从下面的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实现它(如果可以做到):
^{pr2}$
除了
xmlns
,所有标记都没有属性,ElemenTree似乎排除了这个属性,因为它是一些“众所周知”的信息。如果您需要sessionId
标记,您应该已经知道它是http://schemas.ericsson.com/cai3g1.2/名称空间的一部分。在使用
Element.find()
可以使用XPath表达式搜索特定的标记。例如:相关问题 更多 >
编程相关推荐