Parse XML with namespaces in python

Question:

I hope you are doing well.
I really need your help on this one, I´ve been working with XML lately but I came across this XML with Namespaces that I don´t understand.
I want to extract the values of the response but I don´t know how to do it.

This is the XML and I want to extract the values in "item".

        <XMLRESPONSE>
            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns_xsd="http://www.w3.org/2001/XMLSchema" xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns_tns="urn:wsNotes" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                <SOAP-ENV:Body>
                    <ns1:GetPersonasResponse xmlns_ns1="wsNotes">
                        <return xsi_type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:registro[1]">
                            <item xsi_type="tns:registro">
                                <seguro xsi_type="xsd:string">0</seguro>
                                <cedula_pasaporte xsi_type="xsd:string">x-xxx-1454</cedula_pasaporte>
                                <nombre xsi_type="xsd:string">JUANITCO CARDENAS</nombre>
                                <razon_social xsi_type="xsd:string">GOOGLE</razon_social>
                                <patrono xsi_type="xsd:string">GOOGLE PA</patrono>
                                <ruc xsi_nil="true" xsi_type="xsd:string"/>
                                <direccion xsi_type="xsd:string">AVE. MEXICO Y CL. 33 LOCAL. 07</direccion>
                                <telefono1 xsi_type="xsd:int">2259444</telefono1>
                                <telefono2 xsi_nil="true" xsi_type="xsd:int"/>
                                <fecha xsi_type="xsd:string">1220</fecha>
                                <salario xsi_type="xsd:decimal">1000</salario>
                                <promedio_salarial xsi_type="xsd:string">1000</promedio_salarial>
                                <Seis_Meses_Mas xsi_type="xsd:string">Si</Seis_Meses_Mas>
                                <Cantidad_Meses xsi_type="xsd:int">15</Cantidad_Meses>
                                <Historial xsi_type="xsd:string">
                                            Fecha: 1220 Patrono: "APPTIVIDAD" Salario: 5500.00||
                                            Fecha: 1120 Patrono: "APPTIVIDAD" Salario: 5500.00||
                                            Fecha: 0920.0 Patrono: APPTIVIDAD Salario: 5500.00||
                                            Fecha: 0820 Patrono: APPTIVIDAD Salario: 5500.35||
                                            Fecha: 0720 Patrono: APPTIVIDAD Salario: 5500.20||
                                            Fecha: 0620 Patrono: APPTIVIDAD Salario: 5500.01||
                                            Fecha: 0420 Patrono: APPTIVIDAD Salario: 5500.22||
                                            Fecha: 0320 Patrono: APPTIVIDAD Salario: 5500.70||
                                            Fecha: 0120 Patrono: APPTIVIDAD Salario: 5500.97||
                                            Fecha: 1219 Patrono: APPTIVIDAD Salario: 5500.82||
                                            Fecha: 1119 Patrono: APPTIVIDAD Salario: 5500.33||
                                            Fecha: 0919 Patrono: APPTIVIDAD Salario: 5500.25|
                                </Historial>
                                <Total_Empleados xsi_type="xsd:int">20</Total_Empleados>
                            </item>
                        </return>
                    </ns1:GetPersonasResponse>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
        </XMLRESPONSE>

I would appriciate a piece of code on how I could achieve this.

Thanks

Answers:

Note that there is no default namespace defined. Only elements with a prefix such as ns1 and SOAP-ENV are in a namespace. The item element and its children are not in any namespace.

import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
item = tree.find(".//item")
for child in item:
    print(f"{child.tag}: {child.text}")

Output:

seguro: 0
cedula_pasaporte: x-xxx-1454
nombre: JUANITCO CARDENAS
razon_social: GOOGLE
patrono: GOOGLE PA
ruc: None
direccion: AVE. MEXICO Y CL. 33 LOCAL. 07
telefono1: 2259444
telefono2: None
fecha: 1220
salario: 1000
promedio_salarial: 1000
Seis_Meses_Mas: Si
Cantidad_Meses: 15
Historial: 
              Fecha: 1220 Patrono: "APPTIVIDAD" Salario: 5500.00||
              Fecha: 1120 Patrono: "APPTIVIDAD" Salario: 5500.00||
              Fecha: 0920.0 Patrono: APPTIVIDAD Salario: 5500.00||
              Fecha: 0820 Patrono: APPTIVIDAD Salario: 5500.35||
              Fecha: 0720 Patrono: APPTIVIDAD Salario: 5500.20||
              Fecha: 0620 Patrono: APPTIVIDAD Salario: 5500.01||
              Fecha: 0420 Patrono: APPTIVIDAD Salario: 5500.22||
              Fecha: 0320 Patrono: APPTIVIDAD Salario: 5500.70||
              Fecha: 0120 Patrono: APPTIVIDAD Salario: 5500.97||
              Fecha: 1219 Patrono: APPTIVIDAD Salario: 5500.82||
              Fecha: 1119 Patrono: APPTIVIDAD Salario: 5500.33||
              Fecha: 0919 Patrono: APPTIVIDAD Salario: 5500.25|
            
Total_Empleados: 20
Answered By: mzjn

Here’s my solution, hope this helps.

Libraries required: beautifulsoup4 and lxml.

Code:

from bs4 import BeautifulSoup 

# Reading the data inside the xml file
with open('yourFile.xml', 'r') as f:
    data = f.read() 

# Data parsing
bs_data = BeautifulSoup(data, 'xml')

item = bs_data.find("item",{"xsi:type":"tns:registro"})

for child in item:
    if child.name:
        print(f"Tag: {child.name}, Value:{child.next}")

OUTPUT:

Tag: seguro, Value:0
Tag: cedula_pasaporte, Value:x-xxx-1454
Tag: nombre, Value:JUANITCO CARDENAS
Tag: razon_social, Value:GOOGLE
Tag: patrono, Value:GOOGLE PA
Tag: ruc, Value:

Tag: direccion, Value:AVE. MEXICO Y CL. 33 LOCAL. 07
Tag: telefono1, Value:2259444
Tag: telefono2, Value:

Tag: fecha, Value:1220
Tag: salario, Value:1000
Tag: promedio_salarial, Value:1000
Tag: Seis_Meses_Mas, Value:Si
Tag: Cantidad_Meses, Value:15
Tag: Historial, Value:
                                    Fecha: 1220 Patrono: "APPTIVIDAD" Salario: 5500.00||
                                    Fecha: 1120 Patrono: "APPTIVIDAD" Salario: 5500.00||
                                    Fecha: 0920.0 Patrono: APPTIVIDAD Salario: 5500.00||
                                    Fecha: 0820 Patrono: APPTIVIDAD Salario: 5500.35||
                                    Fecha: 0720 Patrono: APPTIVIDAD Salario: 5500.20||
                                    Fecha: 0620 Patrono: APPTIVIDAD Salario: 5500.01||
                                    Fecha: 0420 Patrono: APPTIVIDAD Salario: 5500.22||
                                    Fecha: 0320 Patrono: APPTIVIDAD Salario: 5500.70||
                                    Fecha: 0120 Patrono: APPTIVIDAD Salario: 5500.97||
                                    Fecha: 1219 Patrono: APPTIVIDAD Salario: 5500.82||
                                    Fecha: 1119 Patrono: APPTIVIDAD Salario: 5500.33||
                                    Fecha: 0919 Patrono: APPTIVIDAD Salario: 5500.25|
                        
Tag: Total_Empleados, Value:20
Answered By: Rayster Fernandes