1

i am trying to get data from xml files, using 1 xml file my code works and i have all the data that i need, but when i try to get my code work using diferrents xml files it fails, all the xml have the same elements and in the same positions.

i have tried use glob and list dir with os but is not working and when i execute the python .py from the terminal doesn't return me nothing.

My working code for just one xml file :

import xml.etree.ElementTree as ET
tree = ET.parse('provaDmarcXml2.xml')
root = tree.getroot()
org_name = root[0][0].text


domain = root[1][0].text

LlistaSource_ip = []
LlistaDkim = []
LlistaSpf = []
for source_ip in root.iter('source_ip'):
    sourceIp = source_ip.text
    LlistaSource_ip.append(sourceIp)

for dkim in root.findall("./record/row/policy_evaluated/dkim"):
    Dkim = dkim.text
    LlistaDkim.append(Dkim)


for spf in root.findall("./record/row/policy_evaluated/spf"):
    Spf = spf.text
    LlistaSpf.append(Spf)

for c in range(len(LlistaSource_ip)):
    print (org_name,"\t",end = "")
    print (domain,"\t",end='')
    print (LlistaSource_ip[c],"\t", end="")
    print (LlistaDkim[c],"\t", end="")
    print (LlistaSpf[c],"\t", end="")
    print()

My code doesn't work trying to parse multiple xml files in the same directory.

from os import listdir
import xml.etree.ElementTree as ET


for file in listdir("path to directory"):

    with open(file, "rb"):

        tree = ET.parse(data)

        root = tree.getroot()
        org_name = root[0][0].text
        domain = root[1][0].text

        LlistaSource_ip = []
        LlistaDkim = []
        LlistaSpf = []

        for source_ip in root.iter('source_ip'):
            sourceIp=source_ip.text
            LlistaSource_ip.append(sourceIp)

        for dkim in root.findall("./record/row/policy_evaluated/dkim"):
            Dkim = dkim.text
            LlistaDkim.append(Dkim)

    for spf in root.findall("./record/row/policy_evaluated/spf"):
            Spf = spf.text
        LlistaSpf.append(Spf)

    for c in range(len(LlistaSource_ip)):
        print()

Expected results :

data data data data data
data data data data data data data data data data data data data data data

from all the files

error that i get :

Traceback (most recent call last): File "provaxmlPrograma2.py", line 10, in tree = ET.parse(data) NameError: name 'data' is not defined

or it doesn't return me nothing if i fix this.

0

1 Answer 1

1

There is a little mistake in your open() statement, you have to define it like shown below.

You could also add a check if file.endswith() to make sure you are just trying to parse the xml files of your folder.

from os import listdir
import xml.etree.ElementTree as ET

for file in listdir('path to file'):
    if file.endswith('.xml') or file.endswith('.XML'):
        with open(file, "rb") as data:

            tree = ET.parse(data)
            root = tree.getroot()
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much everyone, I was getting crazy about this and i didn't know why was happening, this fixed my problem. Thanks a lot.

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.