1

I want to fetch all Id tags values from the below xml (SOAP API):

<?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>

Code I wrote is below that gave empty list in the output

  1. when I ran tree.findall('.//Id'), it gave output: []
  2. when I ran print(tree.find('Id')), it gave output: None
  3. when I ran tree.find('Id').text, it gave output:

AttributeError Traceback (most recent call last) in ----> 1 tree.find('Id').text

AttributeError: 'NoneType' object has no attribute 'text'

Code:

>>> 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'
>>>                                                           

**Why am I getting empty list/None type error ? how can I fetch values of ID tags from this xml file ? **

1

1 Answer 1

0

You need to pass namespaces to findall(). There are four namespace in your file.

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)

And this is the result:

CTMSR_1-1036KJ
CTMSR_1-1036SM
CTMSR_1-1036SM
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Thank you so much. My code worked now by including your suggested namespaces. Greatly appreciate all your inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.