0

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

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

3 Comments

It would be a bit more elegant to just iterate the interesting nodes: for col in root.iterfind('.//row/col[1]'):
Thank you very much @kobuz, your answer has been very useful
Thank you very much @Tomalak, your answer has been very useful

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.