通过python解析嵌套xml,给出空列表而不是标记值

2024-06-16 12:13:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从以下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>

下面是我编写的代码,它在输出中给出了空列表

  1. 当我运行tree.findall('.//Id')时,它给出了输出:[]
  2. 当我运行print(tree.find('Id'))时,它给出了输出:None
  3. 当我运行tree.find('Id').text时,它给出了输出:

AttributeError回溯(最近一次呼叫上次) 在里面 ----&燃气轮机;1树。查找('Id')。文本

AttributeError:“非类型”对象没有属性“文本”

代码:

>>> 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标记的值?**


Tags: idtreehttpnumbersitexmlfindschemas
1条回答
网友
1楼 · 发布于 2024-06-16 12:13:34

您需要将名称空间传递给findall()。文件中有四个名称空间

import xml.etree.cElementTree as ElementTree
file_path = 'yourfile.xml'  # change to your file path

tree = ElementTree.parse(file_path)

root = tree.getroot()

namespaces = {"soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
              "eAPI": "http://api.ppdi.com/1.1/Site",
              "infasoapns": "http://schemas.xmlsoap.org/soap/envelope/",
              "infawsdlns": "http://schemas.xmlsoap.org/wsdl/"}

names = root.findall('*/eAPI:getSiteResponse/eAPI:SITE/eAPI:Id', namespaces)  # pass namespaces like this

for name in names:
    print(name.text)

这就是结果:

CTMSR_1-1036KJ
CTMSR_1-1036SM
CTMSR_1-1036SM

相关问题 更多 >