I have XML and I need to extract some values, and if these values don't exists I want "N/A".
<?xml version="1.0" encoding="utf-8"?>
<DEF xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FF.xsd">
<FOLD SERV="TRYING" VERSION="918" PLATFORM="UNIX" GROUP_NAME="UNIX" MODIFIED="False" LAST_UPLOAD="20220117145134UTC" TYPE="1" USED_BY_CODE="0">
<JOB JOBID="835" APP="TRY" JOBNAME="JOBA" DESC="Extrac607" TYPE="db" FORM="Databases" CM_VER="N/A" MULTY_AGENT="N" VERSION_SERIAL="1" PARENT_FOLDER="UNIX">
<VARIABLE NAME="%%TYPE_DB" VALUE="Open Query" />
<VARIABLE NAME="%%APPLOG" VALUE="N" />
<VARIABLE NAME="%%ACCOUNT" VALUE="ONLINE" />
<VARIABLE NAME="%%TYPE" VALUE="Oracle" />
<VARIABLE NAME="%%VERS" VALUE="11g" />
<VARIABLE NAME="%%LENGHT" VALUE="16" />
</JOB>
<JOB JOBID="839" APP="TRY" JOBNAME="JOBB" DESC="Extrac617" TYPE="db" FORM="Databases" CM_VER="N/A" MULTY_AGENT="N" VERSION_SERIAL="1" PARENT_FOLDER="UNIX">
<VARIABLE NAME="%%TYPEDB" VALUE="Open Query" />
<VARIABLE NAME="%%ACCOUNT" VALUE="ONLINE" />
<VARIABLE NAME="%%TYPE" VALUE="Oracle" />
<VARIABLE NAME="%%VERS" VALUE="11g" />
<VARIABLE NAME="%%LENGHT" VALUE="16" />
</JOB>
</FOLD>
</DEF>
Here is my code:
from xml.etree import ElementTree
from contextlib import redirect_stdout
from collections import Counter
import xlsxwriter
import pprint
import os
import datetime
begin_time = datetime.datetime.now()
print(datetime.datetime.now())
###VARIABLES RUTASL
fileXML= 'C:\\xxxx\\completa.xml'
cont=0
path= 'C:\\xxxxxx\\todo.csv'
outputExcel = 'C:\\xxxxxxx\\salida.xlsx'
file2 = 'C:\\xxxxxxxx\\todo.csv'
try:
os.remove(path)
except OSError as e: ## if failed, report it back to the user ##
print ("Error: %s - %s." % (e.filename, e.strerror))
try:
os.remove(outputExcel)
except OSError as e: ## if failed, report it back to the user ##
print ("Error: %s - %s." % (e.filename, e.strerror))
with open(fileXML, encoding="utf8") as f:
tree = ElementTree.parse(f)
# using getchildren() within root and check if tag starts with keyword
#print [node.text for node in root.getchildren() if node.tag.startswith('%%FTP-LPATH')]
root = tree.getroot()
set_tipos= set()
lista_tipos = list()
for node in tree.iter('JOB'):
name = node.attrib.get('JOBNAME')
appl_type = node.attrib.get('TYPE')
appl_form = node.attrib.get('FORM')
if appl_form:
set_tipos.add(appl_form)
lista_tipos.append(appl_form)
else:
set_tipos.add(appl_type)
lista_tipos.append(appl_type)
print('#####################################')
cuenta1 = Counter(lista_tipos)
print(cuenta1)
workbook = xlsxwriter.Workbook(outputExcel)
worksheet = workbook.add_worksheet('Resumen')
bold =workbook.add_format({'bold':1})
centrado =workbook.add_format({'center_across':1})
centrado = workbook.add_format({'bold': True, 'center_across': True})
headings = list()
datos = list()
listaBD = list()
pointDB=1
for key, value in cuenta1.items():
# scores = 0
print(key)
headings.append(key)
datos.append(value)
if 'Databases' in headings:
print('Existe base de datos')
for node4 in tree.iterfind(".//JOB[@TYPE = 'db']"):
listaBD.clear()
name = node4.attrib.get('JOBNAME')
appl_type = node4.attrib.get('TYPE')
listaBD.append(name)
listaBD.append(appl_type)
for node4 in tree.iterfind(".//JOB/VARIABLE[@NAME='%%TYPE_DB']"):
a=0
value = node4.attrib.get('VALUE', 'N/A')
listaBD.append(value)
worksheet.write_row('A'+str(pointDB),listaBD)
pointDB += 1
node4.clear()
else:
print('Don't exists')
print(datetime.datetime.now() - begin_time)
workbook.close()
...................
This works, but I have 6 if(s) in every case, and the program is very slow.
I've edited the main code. It seems problem is the size of xml. XML has 50 MB.
The output is:
How else can I conditionally get these values?