1

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

3
  • What have you tried so far? Commented Sep 27, 2022 at 15:12
  • 1
    docs.python.org/3/library/… Commented Sep 27, 2022 at 15:16
  • I have read the documentation but im not been able to get to the answer and extract the data. Commented Sep 27, 2022 at 15:21

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate. This is what i needed.. To be honest I had never used this new structure, I had always worked with simple xml. Thank you very much
0

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

Comments

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.