I have many XML files, and want to create a DF with the content of them. How can I create a DF to store the "Fundo", "CNPJ" and "Quantidade" from the sample bellow?
EDIT:
Here is a link to download a XML file like the ones I want to read:
https://fnet.bmfbovespa.com.br/fnet/publico/downloadDocumento?id=113925
Here is the code I already Tried:
def informes_tri_xml_read():
Dados = pd.DataFrame([])
folder = os.listdir(DVD_XML)
for file in folder:
#print(file)
try:
if file.endswith('.xml'):
with open(os.path.join(DVD_XML, file), encoding="utf8") as fd:
doc = xmltodict.parse(fd.read())
if 'Emissor' in doc['DadosEconomicoFinanceiros']['InformeTrimestral']['InfoTipoAtivo']['AtivosFinanceiros']['FII'].keys():
NomeFundo = doc['DadosEconomicoFinanceiros']['DadosGerais']['NomeFundo']
CNPJFundo = doc['DadosEconomicoFinanceiros']['DadosGerais']['CNPJFundo']
Mandato = doc['DadosEconomicoFinanceiros']['DadosGerais']['Autorregulacao']['Mandato']
DataTri = doc['DadosEconomicoFinanceiros']['DadosGerais']['DataEncerTrimestre']
FiiNome = doc['DadosEconomicoFinanceiros']['InformeTrimestral']['InfoTipoAtivo']['AtivosFinanceiros'][
'FII']['Emissor']['Fundo']
CNPJ = doc['DadosEconomicoFinanceiros']['InformeTrimestral']['InfoTipoAtivo']['AtivosFinanceiros'][
'FII']['Emissor']['CNPJ']
Quantidade = doc['DadosEconomicoFinanceiros']['InformeTrimestral']['InfoTipoAtivo']['AtivosFinanceiros'][
'FII']['Emissor']['Quantidade']
print(CNPJ)
Problem:
It is printing only the first element with "CNPJ", and not the list of "CNPJ" that is bellow the "Emissor" under "FII".
How can i Iterate through all the "Emissor" to print all the different data under it?