Store return of XML

Asked

Viewed 95 times

1

I have a return of an XML that is a loop (because it’s the only way I know how to call back) but in XML I have two det, det nItem="1" and det nItem="2" which are 2 products, inside them is the tag cEAN which is the one I need separated into different variables to be able to use each individually cEAN.

My call

import xml.etree.ElementTree as ET
tree = ET.parse("C:\\Users\\Expedição\\Videos\\XML\\xml_teste2.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.split())

The XML used:

<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe Id="NFe35200811625533000185550010000043291000219014" versao="4.00">
<ide>
...
</ide>
<emit>
...
</emit>
<dest>
<CNPJ>33737452000100</CNPJ>
<xNome>GP CAVALCANTE COMERCIO, IMPORTACAO E EXPORTACAO DE PECAS AUT</xNome>
<enderDest>
...
</enderDest>
<indIEDest>1</indIEDest>
<IE>799164503118</IE>
<email>[email protected]; [email protected]</email>
</dest>
<det nItem="1">
<prod>
<cProd>ET-CAM 01</cProd>
<cEAN/>
<xProd>CAMERA ESTAC MISTA (FURADA + SUP. BORBOLETA)</xProd>
<NCM>85258029</NCM>
<CEST>2106300</CEST>
<CFOP>5405</CFOP>
<uCom>UNI</uCom>
<qCom>20.0000</qCom>
<vUnCom>22.0000</vUnCom>
<vProd>440.00</vProd>
<cEANTrib/>
<uTrib>UNI</uTrib>
<qTrib>20.0000</qTrib>
<vUnTrib>22.0000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
...
</imposto>
</det>
<det nItem="2">
<prod>
<cProd>ET-CAM 02</cProd>
<cEAN>7898622140258</cEAN>
<xProd>CAMERA ESTAC DIANTEIRA</xProd>
<NCM>85258029</NCM>
<CEST>2106300</CEST>
<CFOP>5405</CFOP>
<uCom>UNI</uCom>
<qCom>10.0000</qCom>
<vUnCom>26.9000</vUnCom>
<vProd>269.00</vProd>
<cEANTrib>7898622140258</cEANTrib>
<uTrib>UNI</uTrib>
<qTrib>10.0000</qTrib>
<vUnTrib>26.9000</vUnTrib>
<indTot>1</indTot>
</prod>

1 answer

0


If you want to process each value individually, it is still possible to do inside the loop. Just check the value of the attribute nItem (assuming that all tags det have this attribute). Something like this:

for det in root.findall('.//nfe:det', ns):
    nItem = det.attrib['nItem']
    EAN = det.find('.//nfe:cEAN', ns).text
    if nItem == '1':
        # cEAN é do nItem 1, fazer o que precisa aqui
    elif nItem == '2':
        # cEAN é do nItem 2, fazer o que precisa aqui

But if you want much have a variable for each, you can search individually for each det:

# busca por <det nItem="1">
det = root.find('.//nfe:det[@nItem="1"]', ns)
if det:
    cean1 = det.find('.//nfe:cEAN', ns).text

# busca por <det nItem="2">
det = root.find('.//nfe:det[@nItem="2"]', ns)
if det:
    cean2 = det.find('.//nfe:cEAN', ns).text

Then you use each variable (cean1 and cean2) as you wish.

But if the idea is to process multiple tags at once (with an amount that may vary), I still think it’s best to do the loop and within it decide what to do (I say this, of course, without knowing the whole context of your problem).

  • 1

    thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.