How to save an xml string to a document?

Asked

Viewed 224 times

0

I want to save my xml which is a str(return of an API) in a . xml file, but when I do this it does not fuciona anyone could help me?

from xml.dom import minidom
cont = minidom.parseString(xml)
arquivo = open(local_save, 'wb')
cont.writexml(arquivo)
arquivo.close()

The function I’m using would be this: All values in it are str, and I want to convert this str to a document . xml

def salvarXML(self, xml, caminho, chNFe, tpEvento, nSeqEvento):
    local_save = caminho + tpEvento + chNFe + nSeqEvento + '-NFe.xml'
    cont = minidom.parseString(xml)
    arquivo = open(local_save, 'wb')
    cont.writexml(arquivo)
    arquivo.close()

The xml that is as string:

 ?xml version="1.0" encoding="utf-8"?>
<NFe>
    <infNFe versao="4.00" Id="NFe43181107364617000135550000000048051004621865">
        <ide>
            <cUF>43</cUF>
            <cNF>00462186</cNF>
            <natOp>VENDA A PRAZO - S</natOp>
            <mod>55</mod>
            <serie>0</serie>
            <nNF>4805</nNF>
            <dhEmi>2018-11-05T09:55:59-02:00</dhEmi>
            <tpNF>1</tpNF>
            <idDest>1</idDest>
            <cMunFG>4305108</cMunFG>
            <tpImp>1</tpImp>
            <tpEmis>1</tpEmis>
            <cDV>5</cDV>
            <tpAmb>2</tpAmb>
            <finNFe>1</finNFe>
            <indFinal>0</indFinal>
            <indPres>9</indPres>
            <procEmi>0</procEmi>
            <verProc>4.00|NS_API</verProc>
        </ide>
        <emit>
            <CNPJ>00000000000000</CNPJ>
            <xNome>Teste de Acentos: áéíóâêãõàèìò �?É�?ÓÂÊÃÕÀÈÌÒ</xNome>
            <enderEmit>
                <xLgr>Rua Bento Osvaldo Trisch</xLgr>
                <nro>.</nro>
                <xCpl></xCpl>
                <xBairro>Pendancino</xBairro>
                <cMun>4303509</cMun>
                <xMun>Caxias do Sul</xMun>
                <UF>RS</UF>
                <CEP>95046600</CEP>
                <fone>0000000000</fone>
            </enderEmit>
            <IE>0000000000</IE>
            <CRT>3</CRT>
        </emit>
        <dest>
            <CNPJ>00000000000000</CNPJ>
            <xNome>NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL</xNome>
            <enderDest>
                <xLgr>AV ANTONIO DURO</xLgr>
                <nro>0</nro>
                <xBairro>OLARIA</xBairro>
                <cMun>4303509</cMun>
                <xMun>CAMAQUA</xMun>
                <UF>RS</UF>
                <CEP>96180000</CEP>
                <cPais>1058</cPais>
                <xPais>BRASIL</xPais>
            </enderDest>
            <indIEDest>1</indIEDest>
            <IE>0000000000</IE>
            <email>[email protected]</email>
        </dest>
        <det nItem="1">
            <prod>
                <cProd>1440859X1_</cProd>
                <cEAN>SEM GTIN</cEAN>
                <xProd>Teste de Acentos: áéíóâêãõàèìò �?É�?ÓÂÊÃÕÀÈÌÒ</xProd>
                <NCM>87089990</NCM>
                <CEST>0107500</CEST>
                <CFOP>5101</CFOP>
                <uCom>UN</uCom>
                <qCom>1.0000</qCom>
                <vUnCom>250.0000</vUnCom>
                <vProd>250.00</vProd>
                <cEANTrib>SEM GTIN</cEANTrib>
                <uTrib>UN</uTrib>
                <qTrib>1.0000</qTrib>
                <vUnTrib>250.0000</vUnTrib>
                <indTot>1</indTot>
                <nItemPed>0</nItemPed>
            </prod>
            <imposto>
                <vTotTrib>50.00</vTotTrib>
                <ICMS>
                    <ICMS10>
                        <orig>0</orig>
                        <CST>10</CST>
                        <modBC>0</modBC>
                        <vBC>250.00</vBC>
                        <pICMS>18.00</pICMS>
                        <vICMS>45.00</vICMS>
                        <vBCFCP>250.00</vBCFCP>
                        <pFCP>2.00</pFCP>
                        <vFCP>5.00</vFCP>
                        <modBCST>4</modBCST>
                        <pMVAST>32.00</pMVAST>
                        <pRedBCST>0.00</pRedBCST>
                        <vBCST>330.00</vBCST>
                        <pICMSST>18.00</pICMSST>
                        <vICMSST>14.40</vICMSST>
                        <vBCFCPST>330.00</vBCFCPST>
                        <pFCPST>2.00</pFCPST>
                        <vFCPST>1.60</vFCPST>
                    </ICMS10>
                </ICMS>
                <IPI>
                    <cEnq>999</cEnq>
                    <IPINT>
                        <CST>53</CST>
                    </IPINT>
                </IPI>
                <PIS>
                    <PISAliq>
                        <CST>01</CST>
                        <vBC>250.00</vBC>
                        <pPIS>1.65</pPIS>
                        <vPIS>4.12</vPIS>
                    </PISAliq>
                </PIS>
                <COFINS>
                    <COFINSAliq>
                        <CST>01</CST>
                        <vBC>250.00</vBC>
                        <pCOFINS>7.60</pCOFINS>
                        <vCOFINS>19.00</vCOFINS>
                    </COFINSAliq>
                </COFINS>
            </imposto>
        </det>
        <total>
            <ICMSTot>
                <vBC>250.00</vBC>
                <vICMS>45.00</vICMS>
                <vICMSDeson>0.00</vICMSDeson>
                <vFCPUFDest>0.00</vFCPUFDest>
                <vICMSUFDest>0.00</vICMSUFDest>
                <vICMSUFRemet>0.00</vICMSUFRemet>
                <vFCP>5.00</vFCP>
                <vBCST>330.00</vBCST>
                <vST>14.40</vST>
                <vFCPST>1.60</vFCPST>
                <vFCPSTRet>0.00</vFCPSTRet>
                <vProd>250.00</vProd>
                <vFrete>0.00</vFrete>
                <vSeg>0.00</vSeg>
                <vDesc>0.00</vDesc>
                <vII>0.00</vII>
                <vIPI>0.00</vIPI>
                <vIPIDevol>0.00</vIPIDevol>
                <vPIS>4.12</vPIS>
                <vCOFINS>19.00</vCOFINS>
                <vOutro>0.00</vOutro>
                <vNF>266.00</vNF>
                <vTotTrib>50.00</vTotTrib>
            </ICMSTot>
        </total>
        <transp>
            <modFrete>1</modFrete>
            <transporta>
                <CNPJ>0000000000000</CNPJ>
                <xNome>EL TESTE</xNome>
                <IE>0000000000</IE>
                <xEnder>ESTR BENTO OSVALDO TRISCH</xEnder>
                <xMun>CAXIAS DO SUL</xMun>
                <UF>RS</UF>
            </transporta>
            <veicTransp>
                <placa>ABC1234</placa>
                <UF>RS</UF>
                <RNTC>12345678</RNTC>
            </veicTransp>
            <vol>
                <qVol>0</qVol>
                <marca>TESTE</marca>
                <nVol>0</nVol>
                <pesoL>0.000</pesoL>
                <pesoB>0.000</pesoB>
            </vol>
        </transp>
        <pag>
            <detPag>
                <tPag>15</tPag>
                <vPag>266.00</vPag>
            </detPag>
            <vTroco>0.00</vTroco>
        </pag>
        <infAdic>
            <infCpl>Teste de Acentos: áéíóâêãõàèìò �?É�?ÓÂÊÃÕÀÈÌÒ DESCONTO PIS 0,01 COFINS 0,06 LEI 11.196 DE 21/11/2005</infCpl>
        </infAdic>
    </infNFe>
</NFe>

I get this xml through this code that Cham my function saves xml:

    if(tpDown.upper().count('X') == 1):
        xml = resposta['xml']
        self.salvarXML(self, xml, caminho, chNFe, '', '')

Json returned by API:

{'status': 200, 'motivo': 'Consulta realizada com sucesso', 'chNFe': '43181107364617000135550000000048051004621865', 'xml':[...]
  • Matheus, where does the error occur? Which problem is having?

  • I want to download on the computer the xml that comes from the API, from it I get a str, like:'<nfeProc>[...]'. Then I get this string step in this method: def salvarXML(self, xml, caminho, chNFe, tpEvento, nSeqEvento):&#xA; local_save = caminho + tpEvento + chNFe + nSeqEvento + '-NFe.xml'&#xA; cont = minidom.parseString(xml)&#xA; arquivo = open(local_save, 'wb')&#xA; cont.writexml(arquivo) file.close()

  • I basically just want to save this xml to an . xml file, so

  • I advise you to edit your question and put details like the function you are using, the error log and also an example of xml you want saved, it is better to try to help like this :)

  • I don’t have the logs only, want some more information?

  • If you receive an XML from the API and just want to save the response, why not save directly instead of trying to parse the file? Example

  • xml is one of the parameters within the json response API, I will edit to show how I got xml str

  • No longer necessary I found my mistake hahah

  • Thank you all :)

  • If possible, post the answer. So other users can be helped if they have the same problem ;)

  • Sure! No problem, I forgot to post

Show 6 more comments

1 answer

1


I took the xml str and I just found it in an open file 'Wb', so that it could be saved bytes without problems, as we can see in this my function:

def salvarXML(self, xml, caminho, chNFe, tpEvento, nSeqEvento):
    local_save = caminho + tpEvento + chNFe + nSeqEvento + '-NFe.xml'
    arquivo = open(local_save, 'wb')
    arquivo.write(str(xml).encode())
    arquivo.close()
    return

Browser other questions tagged

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