Passing xml to String

1

Greetings I am trying to pass an xml to string in python I have the following code:

from lxml import etree
xml_str = etree.tostring('media/Facturacion/Comprobantes Generados/2012201800117924424190011001001000000002123456785.xml', encoding='utf8', method='xml')
print xml_str

The error that generates me is the following:

  

Type 'str' can not be serialized.

I do not know what the error could be, maybe someone who tells me what the error is, what I'm doing wrong, thanks in advance. The structure of xml is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<factura id="comprobante" version="1.0.0">
    <infoTributaria>
        <ambiente>1</ambiente>
        <tipoEmision>1</tipoEmision>
        <razonSocial>TEST CIA. LTDA.</razonSocial>
        <nombreComercial>TEST</nombreComercial>
        <ruc>1794642419001</ruc>
        <claveAcceso>2012201800117924424190011001001000000002123856785</claveAcceso>
        <codDoc>01</codDoc>
        <estab>001</estab>
        <ptoEmi>002</ptoEmi>
        <secuencial>000000002</secuencial>
        <dirMatriz>TEST</dirMatriz>
    </infoTributaria>
    <infoFactura>
        <fechaEmision>20/12/2018</fechaEmision>
        <dirEstablecimiento>TEST</dirEstablecimiento>
        <obligadoContabilidad>SI</obligadoContabilidad>
        <tipoIdentificacionComprador>04</tipoIdentificacionComprador>
        <razonSocialComprador>Fernanda Paredes</razonSocialComprador>
        <identificacionComprador>1802626000001</identificacionComprador>
        <direccionComprador>Av Cevallos y Martinez</direccionComprador>
        <totalSinImpuestos>7.50</totalSinImpuestos>
        <totalDescuento>0.15</totalDescuento>
        <totalConImpuestos>
            <totalImpuesto>
                <codigo>2</codigo>
                <codigoPorcentaje>2</codigoPorcentaje>
                <baseImponible>7.50</baseImponible>
                <tarifa>12</tarifa>
                <valor>0.90</valor>
            </totalImpuesto>
        </totalConImpuestos>
        <propina>0.00</propina>
        <importeTotal>8.25</importeTotal>
        <moneda>DOLAR</moneda>
        <pagos>
            <pago>
                <formaPago>01</formaPago>
                <total>8.25</total>
            </pago>
        </pagos>
    </infoFactura>
    <detalles>

        <detalle>
            <codigoPrincipal>123456787</codigoPrincipal>
            <codigoAuxiliar>juanvaldez300</codigoAuxiliar>
            <descripcion>Cafe Juan Valdez</descripcion>
            <cantidad>1</cantidad>
            <precioUnitario>7.50</precioUnitario>
            <descuento>0</descuento>
            <precioTotalSinImpuesto>7.50</precioTotalSinImpuesto>
            <impuestos>
                <impuesto>
                    <codigo>2</codigo>
                    <codigoPorcentaje>2</codigoPorcentaje>
                    <tarifa>12</tarifa>
                    <baseImponible>7.50</baseImponible>
                    <valor>0.90</valor>
                </impuesto>
            </impuestos>
        </detalle>

    </detalles>
    <infoAdicional>
        <campoAdicional nombre="Dirección">Av Cevallos y Martinez</campoAdicional>
        <campoAdicional nombre="Teléfono">32856974</campoAdicional>
        <campoAdicional nombre="Email">[email protected]</campoAdicional>
    </infoAdicional>
</factura>
    
asked by Diego Avila 03.01.2019 в 02:04
source

1 answer

1

What happens is that etree.tostring() wait as the first parameter, according to the documentation, an XML element, and what you are going through is a string that represents the path to a file. You should first read the file and interpret it as an XML element, for this you can use etree.parse() . Your example, it should be something like this:

from lxml import etree

tree = etree.parse('media/Facturacion/Comprobantes Generados/2012201800117924424190011001001000000002123456785.xml')
xml_str = etree.tostring(tree, encoding='utf8', method='xml')

print xml_str
    
answered by 03.01.2019 / 03:10
source