Get a specific XML field

Asked

Viewed 158 times

-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)

1 answer

1


with the help of the xmltodict library available at link you can perform the xml parser for Dict or json and also perform the return path. also created a gist to expand this code a little.

#dependências
import xmltodict



my_xml = """
<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>
                <det nItem="1">
                    <prod>
                        <cProd>01.304</cProd>
                        <cEAN>7898338312758</cEAN>
                        <xProd>AF QUADRIAXIAL 6 QR6 HURRICANE</xProd>
                        <NCM>85182100</NCM>
                        <CEST>0105700</CEST>
                    </prod>
                </det>
                <det nItem="2">
                    <prod>
                        <cProd>01.304</cProd>
                        <cEAN>7898338312758</cEAN>
                        <xProd>AF QUADRIAXIAL 6 QR6 HURRICANE</xProd>
                        <NCM>85182100</NCM>
                        <CEST>0105700</CEST>
                    </prod>
                </det>
            </ide>
        </infNFe>
    </NFe>
</nfeProc>
"""
#parse
my_dict = xmltodict.parse(my_xml)

#dicionario
print(my_dict)

#acesso aos elementos do dicionário, para os que tem atributos se utiliza list:
print (my_dict['nfeProc']['NFe']['infNFe']['ide']['det'][0]['prod']['cEAN'])

out:

OrderedDict([('nfeProc', OrderedDict([('@xmlns', 'http://www.portalfiscal.inf.br/nfe'), ('@versao', '4.00'), ('NFe', OrderedDict([('@xmlns', 'http://www.portalfiscal.inf.br/nfe'), ('infNFe', OrderedDict([('@versao', '4.00'), ('@Id', 'NFe35200823520039000135550010000031451090031450'), ('ide', OrderedDict([('det', [OrderedDict([('@nItem', '1'), ('prod', OrderedDict([('cProd', '01.304'), ('cEAN', '7898338312758'), ('xProd', 'AF QUADRIAXIAL 6 QR6 HURRICANE'), ('NCM', '85182100'), ('CEST', '0105700')]))]), OrderedDict([('@nItem', '2'), ('prod', OrderedDict([('cProd', '01.304'), ('cEAN', '7898338312758'), ('xProd', 'AF QUADRIAXIAL 6 QR6 HURRICANE'), ('NCM', '85182100'), ('CEST', '0105700')]))])])]))]))]))]))])

7898338312758

Browser other questions tagged

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