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.