Mariano Munarriz
Mariano Munarriz

Reputation: 91

Extract data from an XML string with Python

I have an XML string and I need to extract only the first "col" tag in each group of "row". In other words, the output should be the following:

Fluk-TEST
Beltran-TEST
ACME-TEST

This is the XML:

data = '''<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <processOXIMessageResponse xmlns="urn:com:singun:webservice" xmlns:ns="urn:com:singun:webservice">
         <ns1:processOXIMessageReturn xmlns:ns1="urn:com:singun:webservice">
            <SingunDocument xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" protocol="OXI">
               <sessionId xmlns="">1613762599483</sessionId>
               <command xmlns="" echo="" xsi:type="ServiceProviderGetListResponse">
                  <serviceProviderTable>
                     <colHeading>Service Provider Id</colHeading>
                     <colHeading>Service Provider Name</colHeading>
                     <colHeading>Is Enterprise</colHeading>
                     <colHeading>Reseller Id</colHeading>
                     <row>
                        <col>Fluk-TEST</col>
                        <col>ATP 1</col>
                        <col>true</col>
                        <col />
                     </row>
                     <row>
                        <col>Beltran-TEST</col>
                        <col>ATP 2</col>
                        <col>true</col>
                        <col />
                     </row>
                     <row>
                        <col>ACME-TEST</col>
                        <col>ATP 3</col>
                        <col>true</col>
                        <col />
                     </row>
                  </serviceProviderTable>
               </command>
            </SingunDocument>
         </ns1:processOXIMessageReturn>
      </processOXIMessageResponse>
   </soapenv:Body>
</soapenv:Envelope>

The following is my code, but it extracts all the "col" tags instead of just the first of each group as I would like:

import xml.etree.ElementTree as ET

root = ET.fromstring(data)
for row in root.iter('row'):
    for col in row:
        print(col.text)

This is the output of my code:

Fluk-TEST
ATP 1
true
None
Beltran-TEST
ATP 2
true
None
ACME-TEST
ATP 3
true
None

Please if you can give me a hand. Thanks a lot

Upvotes: 0

Views: 73

Answers (1)

kobuz
kobuz

Reputation: 231

You can use the slice operator. It takes up to one element from list-like object:

for row in root.iter('row'):
    for col in row[:1]:
        print(col.text)

There's also this high level SOAP library but it might be an overkill in this case https://docs.python-zeep.org/en/master/

Upvotes: 2

Related Questions