【发布时间】:2017-06-09 13:02:10
【问题描述】:
我正在调用一个 SOAP Web 服务,它返回一个类似于下面的 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>
<SalidaConsultaSAF xmlns="http://wtp">
<coderror>02</coderror>
<dserror>No records</dserror>
</SalidaConsultaSAF>
</soapenv:Body>
</soapenv:Envelope>
我正在尝试解析它以访问标签 coderror 和 dserror 的值,但我只是一直在惨败。
这只是我采取的几种方法之一:
# parse XML response
from xml.etree import ElementTree as ET
# call the web service
response = requests.post(url, auth=('myUser', 'myPass'),
verify=False, data=body, headers=headers)
tree = ET.ElementTree(ET.fromstring(response.content))
a_ = ET.Element(tree) # ==> {'attrib': {}, 'tag': <xml.etree.ElementTree.ElementTree object at 0x7f4736e299d0>, '_children': []}
b_ = ET.SubElement(a_, 'coderror') # ==> {'attrib': {}, 'tag': 'soapenv', '_children': []}
如您所见,变量b 的值为“空”。我想获取字符串02。
有什么帮助吗?
【问题讨论】:
标签: python xml elementtree