I'm using xml.etree.ElementTree
My XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.0668.011.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02 pain.0668.011.02.xsd">
<CstmrDrctDbtInitn>
<GrpHdr>
<MsgId>cca82067341ea996fc89c17afb5</MsgId>
<CreDtTm>2021-07-10T23:03:11Z</CreDtTm>
<NbOfTxs>64</NbOfTxs>
<CtrlSum>1334.82</CtrlSum>
<InitgPty>
<Nm>Example Company</Nm>
</InitgPty>
</GrpHdr>
<PmtInf>
<PmtInfId>cca82067341ea996fc89c17afb5</PmtInfId>
<PmtMtd>EN</PmtMtd>
<NbOfTxs>64</NbOfTxs>
<CtrlSum>1334.82</CtrlSum>
<PmtTpInf>
<SvcLvl>
<Cd>NORM</Cd>
</SvcLvl>
<LclInstrm>
<Cd>MAIN</Cd>
</LclInstrm>
<SeqTp>RCUR</SeqTp>
</PmtTpInf>
</PmtInf>
</CstmrDrctDbtInitn>
</Document>
I want to get all values from Element CtrlSum and write them into a dataframe.
My problem is the <Document xmlns...>
Tag, which currently prevents me from accessing other values.
I have tried to access the all elements with
tree = et.parse('xml_beispiel.xml')
root = tree.getroot()
root.findall(".")
or
root.findall(".//GrpHdr")
but only get empty values or the values from <Document xmlns...>
You have to deal with your namespaces; try this:
ns = {'': 'urn:iso:std:iso:20022:tech:xsd:pain.0668.011.02'}
root.find('.//GrpHdr/CtrlSum',namespaces=ns).text
and see if it works.