Page 1 of 1

CREAR ARCHIVO XML

Posted: Mon Apr 27, 2026 2:45 pm
by juan fco. vasquez Mendoza

Buenas, aquí tengo este formato para el ARCHIVO XML de mi país República Dominicana, para quien ya con sus experiencias pueda colaborar con ayuda.

agradecido siempre,
JFVM

Esquema Base de un e-CF XML
xml
<ECF xmlns="http://dgii.gov.do">
<Encabezado>
<IdDoc>
<TipoeCF>31</TipoeCF> <!-- 31: Factura de Crédito Fiscal Electrónica -->
<eNCF>E310000000001</eNCF>
<FechaEmision>2026-04-26</FechaEmision>
</IdDoc>
<Emisor>
<RNCEmisor>101XXXXXX</RNCEmisor>
<RazonSocialEmisor>Nombre de tu Empresa</RazonSocialEmisor>
</Emisor>
<Receptor>
<RNCReceptor>130XXXXXX</RNCReceptor>
<RazonSocialReceptor>Nombre del Cliente</RazonSocialReceptor>
</Receptor>
<Totales>
<MontoTotal>1180.00</MontoTotal>
<MontoITBIS>180.00</MontoITBIS>
</Totales>
</Encabezado>
<DetallesItems>
<Item>
<IndicadorExentoITBIS>1</IndicadorExentoITBIS>
<NombreItem>Producto de Ejemplo</NombreItem>
<CantidadItem>1</CantidadItem>
<PrecioUnitarioItem>1000.00</PrecioUnitarioItem>
<MontoItem>1000.00</MontoItem>
</Item>
</DetallesItems>
<!-- La sección Signature es obligatoria y se genera tras firmar el XML -->
<Signature xmlns="http://w3.org">
... (Bloque de firma digital) ...
</Signature>
</ECF>


Re: CREAR ARCHIVO XML

Posted: Mon Apr 27, 2026 6:22 pm
by karinha

Mira este ejemplo simples se ayuda,

Code: Select all | Expand

// C:\FHW2603\SAMPLES\CREAXML2.PRG - Build 12 de FWH26.03

#include "FiveWin.ch"
#include "hbclass.ch"
#include "hbxml.ch"
#include "FileIO.ch"

FUNCTION Main() // WriteXML()

   LOCAL hFile

   hFile := FCreate( "ejemplo.xml" )
   
IF hFile != -1 FWrite( hFile, '<?xml version="1.0" encoding="UTF-8"?>' + HB_OsNewLine() ) FWrite( hFile, '<root>' + HB_OsNewLine() ) FWrite( hFile, ' <item id="1">Data</item>' + HB_OsNewLine() ) FWrite( hFile, '</root>' + HB_OsNewLine() ) FWrite( hFile, '<person dataSource="215" dataSourceNbr="215">' + HB_OsNewLine() ) FWrite( hFile, '<active>Y</active>' + HB_OsNewLine() ) FWrite( hFile, '<Item>' + HB_OsNewLine() ) FWrite( hFile, '<IndicadorExentoITBIS>1</IndicadorExentoITBIS>' + HB_OsNewLine() ) FWrite( hFile, '<NombreItem>Producto de Ejemplo</NombreItem>' + HB_OsNewLine() ) FWrite( hFile, '<CantidadItem>1</CantidadItem>' + HB_OsNewLine() ) FWrite( hFile, '<PrecioUnitarioItem>1000.00</PrecioUnitarioItem>' + HB_OsNewLine() ) FWrite( hFile, '<MontoItem>1000.00</MontoItem>' + HB_OsNewLine() ) FWrite( hFile, '</Item>' ) FClose( hFile ) ENDIF RETURN NIL

Regards, saludos.


Re: CREAR ARCHIVO XML

Posted: Mon Apr 27, 2026 6:32 pm
by karinha

Creo que solo funcione con HARBOUR. Con xHarbour, NO COMPILA.

Code: Select all | Expand

#include "FiveWin.ch"
#include "hbxml.ch"

Function Main()
    Local oXmlWriter
    Local cArquivo := "exemplo.xml"
    Local cEmpresa := "Minha Empresa LTDA"
    Local cCNPJ    := "00.000.000/0001-00"

// 1. Criar o arquivo e iniciar o writer
oXmlWriter := TXmlWriter():New( cArquivo )

// 2. Iniciar o Documento XML (prologue)
oXmlWriter:Start( "1.0", "UTF-8" )

// 3. Abrir a tag raiz (root)
oXmlWriter:OpenTag( "cadastro" )

// 4. Adicionar elementos (tags)
oXmlWriter:Element( "nome_empresa", cEmpresa )
oXmlWriter:Element( "cnpj", cCNPJ )

// 5. Exemplo de estrutura aninhada (tags filhas)
oXmlWriter:OpenTag( "endereco" )
oXmlWriter:Element( "rua", "Rua das Flores, 123" )
oXmlWriter:Element( "cidade", "São Paulo" )
oXmlWriter:CloseTag( "endereco" )

// 6. Fechar a tag raiz
oXmlWriter:CloseTag( "cadastro" )

// 7. Finalizar e salvar
oXmlWriter:End()
oXmlWriter:Destroy()

MsgInfo( "XML gerado com sucesso: " + cArquivo )

Return Nil

Regards, saludos.


Re: CREAR ARCHIVO XML

Posted: Mon Apr 27, 2026 8:59 pm
by Antonio Linares

Code: Select all | Expand

// TXmlWriter — minimal streaming XML writer for Harbour / xHarbour.
// Designed for the typical OpenTag / Element / CloseTag pattern. Writes
// directly to disk so it stays cheap on memory for large documents.

#include "FileIO.ch"

#define CRLF Chr(13)+Chr(10)

CLASS TXmlWriter

   DATA cFile     AS CHARACTER
   DATA hFile     AS NUMERIC    INIT -1
   DATA nIndent   AS NUMERIC    INIT 0
   DATA cIndStr   AS CHARACTER  INIT "  "      // 2 spaces per level
   DATA aStack    AS ARRAY      INIT {}        // tag stack for nesting
   DATA lOpen     AS LOGICAL    INIT .F.

   METHOD New( cFile ) CONSTRUCTOR
   METHOD Start( cVersion, cEncoding )
   METHOD OpenTag( cTag, hAttribs )
   METHOD CloseTag( cTag )
   METHOD Element( cTag, cValue, hAttribs )
   METHOD Comment( cText )
   METHOD CData( cText )
   METHOD End()
   METHOD Destroy()

   METHOD Write( cText ) PROTECTED
   METHOD WriteIndent()  PROTECTED
   METHOD AttrStr( hAttribs )                  PROTECTED
   METHOD Escape( cText )                      PROTECTED

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( cFile ) CLASS TXmlWriter
   ::cFile  := cFile
   ::hFile  := FCreate( cFile )
   ::lOpen  := ( ::hFile != -1 )
   ::aStack := {}
   ::nIndent := 0
RETURN Self

//----------------------------------------------------------------------------//

METHOD Start( cVersion, cEncoding ) CLASS TXmlWriter
   DEFAULT cVersion  := "1.0"
   DEFAULT cEncoding := "UTF-8"
   ::Write( '<?xml version="' + cVersion + '" encoding="' + cEncoding + '"?>' + CRLF )
RETURN Self

//----------------------------------------------------------------------------//

METHOD OpenTag( cTag, hAttribs ) CLASS TXmlWriter
   ::WriteIndent()
   ::Write( "<" + cTag + ::AttrStr( hAttribs ) + ">" + CRLF )
   AAdd( ::aStack, cTag )
   ::nIndent++
RETURN Self

//----------------------------------------------------------------------------//

METHOD CloseTag( cTag ) CLASS TXmlWriter
   LOCAL n
   IF ::nIndent > 0
      ::nIndent--
   ENDIF
   IF Len( ::aStack ) > 0
      n := Len( ::aStack )
      DEFAULT cTag := ::aStack[ n ]
      ASize( ::aStack, n - 1 )
   ENDIF
   ::WriteIndent()
   ::Write( "</" + cTag + ">" + CRLF )
RETURN Self

//----------------------------------------------------------------------------//

METHOD Element( cTag, cValue, hAttribs ) CLASS TXmlWriter
   DEFAULT cValue := ""
   ::WriteIndent()
   IF Empty( cValue )
      ::Write( "<" + cTag + ::AttrStr( hAttribs ) + "/>" + CRLF )
   ELSE
      ::Write( "<" + cTag + ::AttrStr( hAttribs ) + ">" + ;
               ::Escape( cValue ) + "</" + cTag + ">" + CRLF )
   ENDIF
RETURN Self

//----------------------------------------------------------------------------//

METHOD Comment( cText ) CLASS TXmlWriter
   ::WriteIndent()
   ::Write( "<!-- " + cText + " -->" + CRLF )
RETURN Self

//----------------------------------------------------------------------------//

METHOD CData( cText ) CLASS TXmlWriter
   ::WriteIndent()
   ::Write( "<![CDATA[" + cText + "]]>" + CRLF )
RETURN Self

//----------------------------------------------------------------------------//

METHOD End() CLASS TXmlWriter
   // Flush any remaining open tags so the file is well-formed even if the
   // caller forgot to close them.
   DO WHILE Len( ::aStack ) > 0
      ::CloseTag()
   ENDDO
RETURN Self

//----------------------------------------------------------------------------//

METHOD Destroy() CLASS TXmlWriter
   IF ::lOpen
      FClose( ::hFile )
      ::lOpen := .F.
      ::hFile := -1
   ENDIF
RETURN Self

//----------------------------------------------------------------------------//

METHOD Write( cText ) CLASS TXmlWriter
   IF ::lOpen
      FWrite( ::hFile, cText )
   ENDIF
RETURN NIL

//----------------------------------------------------------------------------//

METHOD WriteIndent() CLASS TXmlWriter
   LOCAL i
   FOR i := 1 TO ::nIndent
      ::Write( ::cIndStr )
   NEXT
RETURN NIL

//----------------------------------------------------------------------------//

METHOD AttrStr( hAttribs ) CLASS TXmlWriter
   LOCAL cOut := ""
   LOCAL cKey
   IF HB_ISHASH( hAttribs )
      FOR EACH cKey IN hb_HKeys( hAttribs )
         cOut += " " + cKey + '="' + ::Escape( cValToChar( hAttribs[ cKey ] ) ) + '"'
      NEXT
   ENDIF
RETURN cOut

//----------------------------------------------------------------------------//

METHOD Escape( cText ) CLASS TXmlWriter
   cText := StrTran( cText, "&",  "&amp;" )
   cText := StrTran( cText, "<",  "&lt;" )
   cText := StrTran( cText, ">",  "&gt;" )
   cText := StrTran( cText, '"',  "&quot;" )
   cText := StrTran( cText, "'",  "&apos;" )
RETURN cText

Re: CREAR ARCHIVO XML

Posted: Tue Apr 28, 2026 1:58 pm
by karinha

Thank you very much super master Antônio.

Download completo:

https://mega.nz/file/Nc1VUQaQ#NsxpV7_Kf ... xt3T6gvvVA

Gracias, tks.

Regards, saludos.


Re: CREAR ARCHIVO XML

Posted: Tue Apr 28, 2026 3:23 pm
by juan fco. vasquez Mendoza

Gracias Karina

Gracias Maestro Antonio, iba a comentar que faltaba esa CLASE para el ejemplo que mostró Karinha.

Namaste