Skip to content

toXml()

Serializes a document at any stage (created, stamped or signed) to XML that follows the SII schema.

Basic Usage#

TypeScript
import { toXml } from 'dte-sdk'
 
const xml = toXml(dte, { pretty: true })

For the invoice from createDte(), that produces:

xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<DTE version="1.0">
  <Documento ID="F1024T33">
    <Encabezado>
      <IdDoc>
        <TipoDTE>33</TipoDTE>
        <Folio>1024</Folio>
        <FchEmis>2026-10-28</FchEmis>
      </IdDoc>
      <Emisor>
        <RUTEmisor>76123456-0</RUTEmisor>
        <RznSoc>Arrow Software SpA</RznSoc>
        <GiroEmis>Desarrollo de software</GiroEmis>
        <Acteco>620200</Acteco>
        <DirOrigen>Av. Providencia 1234</DirOrigen>
        <CmnaOrigen>Providencia</CmnaOrigen>
      </Emisor>
      <Receptor>
        <RUTRecep>77654321-7</RUTRecep>
        <RznSocRecep>Comercial Los Andes Ltda.</RznSocRecep>
        <GiroRecep>Comercio al por menor</GiroRecep>
        <DirRecep>Huérfanos 1055</DirRecep>
        <CmnaRecep>Santiago</CmnaRecep>
      </Receptor>
      <Totales>
        <MntNeto>120000</MntNeto>
        <TasaIVA>19</TasaIVA>
        <IVA>22800</IVA>
        <MntTotal>142800</MntTotal>
      </Totales>
    </Encabezado>
    <Detalle>
      <NroLinDet>1</NroLinDet>
      <NmbItem>Licencia anual</NmbItem>
      <QtyItem>1</QtyItem>
      <PrcItem>120000</PrcItem>
      <MontoItem>120000</MontoItem>
    </Detalle>
  </Documento>
</DTE>

Type Signature#

TypeScript
function toXml(
  doc: Dte | StampedDte | SignedDte,
  options?: { pretty?: boolean; encoding?: 'ISO-8859-1' | 'UTF-8' },
): string

Parameters#

  • doc (Dte | StampedDte | SignedDte) - A document at any stage of the lifecycle.
  • options (optional)
    • pretty (boolean, default: false) - Indent the output for reading. Ignored for signed documents (see below).
    • encoding ('ISO-8859-1' | 'UTF-8', default: 'ISO-8859-1') - The encoding named in the XML declaration. The default is the one the SII expects.

Return Value#

Returns the XML as a string. What it contains depends on the stage:

InputOutput
DteThe <DTE> with its <Documento>: header, totals and detail lines. Useful for previews and tests, not for sending.
StampedDteThe same, plus the <TED> stamp and its timestamp inside <Documento>.
SignedDteThe final signed document, with the XMLDSig <Signature>. Identical to signed.xml.

toXml() doesn't throw for documents built with dte-sdk: they were already validated by createDte().

Signed Documents#

A signature covers the exact bytes it signed. Re-indenting a signed document would invalidate it, so pretty is ignored for a SignedDte and you always get the document exactly as it was signed.

Encoding#

A JavaScript string has no encoding of its own. The encoding option only sets the XML declaration, so encode the bytes to match when you write the file or send it over HTTP:

TypeScript
import { writeFile } from 'node:fs/promises'
import { toXml } from 'dte-sdk'
 
// ISO-8859-1 (default): write the bytes as latin1
await writeFile(`dte-33-${signed.folio}.xml`, toXml(signed), 'latin1')
 
// UTF-8: the declaration says UTF-8, write it as UTF-8
await writeFile('preview.xml', toXml(dte, { encoding: 'UTF-8' }), 'utf8')

Writing ISO-8859-1 XML as UTF-8 by mistake garbles accented characters such as the é in Huérfanos.

Examples#

Archive What You Sent#

Keep the signed XML of every document you issue, alongside its track id:

TypeScript
import { sendToSii, toXml } from 'dte-sdk'
 
const { trackId } = await sendToSii(signed, siiOptions)
 
await db.documents.insert({
  type: signed.type,
  folio: signed.folio,
  trackId,
  xml: toXml(signed),
})

Preview a Draft#

TypeScript
import { createDte, toXml } from 'dte-sdk'
 
const draft = createDte(input)
 
console.log(toXml(draft, { pretty: true, encoding: 'UTF-8' }))

Snapshot Tests#

TypeScript
import { createDte, stampDte, toXml } from 'dte-sdk'
 
test('invoice 1024 serializes as expected', () => {
  const stamped = stampDte(createDte(input), caf, {
    stampedAt: new Date('2026-10-28T10:15:00-03:00'),
  })
  expect(toXml(stamped, { pretty: true })).toMatchSnapshot()
})