-1
I’m creating a program in python
which aims to get data from an XML, I am using xml.etree.Elementtree for this, I am using a for
to call the fields cEAN
I need, but the for
returns an element list plus I want only one element at a time, how can I do this?
The XML used:
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe versao="4.00" Id="NFe35200823520039000135550010000031451090031450">
<ide>
...
</ide>
<emit>
...
</emit>
<dest>
...
</dest>
<det nItem="1">
<prod>
<cProd>01.304</cProd>
<cEAN>7898338312758</cEAN>
<xProd>AF QUADRIAXIAL 6 QR6 HURRICANE</xProd>
<NCM>85182100</NCM>
<CEST>0105700</CEST>
<CFOP>5405</CFOP>
<uCom>par</uCom>
<qCom>60.0000</qCom>
<vUnCom>53.6000000000</vUnCom>
<vProd>3216.00</vProd>
<cEANTrib>7898338312758</cEANTrib>
<uTrib>par</uTrib>
<qTrib>60.0000</qTrib>
<vUnTrib>53.6000000000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
...
</imposto>
<infAdProd>7898338312758</infAdProd>
</det>
<det nItem="2">
...
</det>
<det nItem="3">
...
</det>
<det nItem="4">
...
</det>
<det nItem="5">
...
</det>
<det nItem="6">
...
</det>
<det nItem="7">
...
</det>
<det nItem="8">
...
</det>
<det nItem="9">
...
</det>
<det nItem="10">
...
</det>
<det nItem="11">
...
</det>
What I’ve tried to do:
import xml.etree.ElementTree as ET
tree = ET.parse("C:\\Users\\Expedição\\Videos\\XML\\xml_teste1.xml")
root = tree.getroot()
ns = {'nfe': 'http://www.portalfiscal.inf.br/nfe'}
for det in root.findall('.//nfe:det', ns):
quantidade = det.find('.//nfe:qCom', ns).text
EAN = det.find('.//nfe:cEAN', ns).text
print( EAN)