【发布时间】:2021-04-27 17:51:47
【问题描述】:
我想从下面的 xml (SOAP API) 中获取所有 Id 标签值:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<infasoapns:Body xmlns:eAPI="http://api.ppdi.com/1.1/Site" xmlns:infasoapns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:infawsdlns="http://schemas.xmlsoap.org/wsdl/">
<eAPI:getSiteResponse>
<eAPI:SITE>
<eAPI:Id>CTMSR_1-1036KJ</eAPI:Id>
<eAPI:Sponsor>Ell Inc</eAPI:Sponsor>
<eAPI:CRO>PDP</eAPI:CRO>
<eAPI:Protocol_Number>EL184-308</eAPI:Protocol_Number>
<eAPI:Protocol_Id>CTMSR_1-LCXB0</eAPI:Protocol_Id>
</eAPI:SITE>
<eAPI:SITE>
<eAPI:Id>CTMSR_1-1036SM</eAPI:Id>
<eAPI:Sponsor>Ell Inc</eAPI:Sponsor>
<eAPI:CRO>PDP</eAPI:CRO>
<eAPI:Protocol_Number>EL184-308</eAPI:Protocol_Number>
<eAPI:Protocol_Id>CTMSR_1-LCXB0</eAPI:Protocol_Id>
</eAPI:SITE>
<eAPI:SITE>
<eAPI:Id>CTMSR_1-1036SM</eAPI:Id>
<eAPI:Sponsor>Ell Inc</eAPI:Sponsor>
<eAPI:CRO>PDP</eAPI:CRO>
<eAPI:Protocol_Number>EL184-308</eAPI:Protocol_Number>
<eAPI:Protocol_Id>CTMSR_1-LCXB0</eAPI:Protocol_Id>
</eAPI:SITE>
</eAPI:getSiteResponse>
</infasoapns:Body>
</soapenv:Envelope>
我写的代码在下面,在输出中给出了空列表
- 当我运行 tree.findall('.//Id') 时,它给出了输出:[]
- 当我运行 print(tree.find('Id')) 时,它给出了输出:无
- 当我运行 tree.find('Id').text 时,它给出了输出:
AttributeError Traceback(最近一次调用最后一次) 在 ----> 1 tree.find('Id').text
AttributeError: 'NoneType' 对象没有属性 'text'
代码:
>>> import xml.etree.cElementTree as ElementTree
>>> file_path = 'C:\\Users\\dshukla\\Desktop\\docs\\PPD project\\Response\\WS_SITES_1.1_RES'
>>> tree = ElementTree.parse(file_path)
>>> root = tree.getroot()
>>> print(root)
<Element '{http://schemas.xmlsoap.org/soap/envelope/}Envelope' at 0x00000238F6BFCD10>
>>> tree.findall('.//Id')
[]
>>> tree.find('Id')
>>> print(tree.find('Id'))
None
>>> tree.find('Id').text
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'text'
>>>
**为什么我会收到空列表/无类型错误?如何从这个 xml 文件中获取 ID 标签的值? **
【问题讨论】:
-
这能回答你的问题吗? How to parse SOAP XML with Python?
标签: python xml soap xml-parsing elementtree