FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour change CODEPAGE obtained from XML
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
change CODEPAGE obtained from XML
Posted: Fri May 08, 2015 06:42 PM
I'm trying to access a webservice that works with encoding="ISO-8859-1"

The problem is that the return has special characters and is generating divergence in the data.

XML Header

<?xml version="1.0" encoding="ISO-8859-1" ?>

In the browser:

<nomeSegmento>Cacau em Pó</nomeSegmento>

When creating a TXT with the return of XML to treat the return:
<nomeSegmento>Cacau em P?omeSegmento>
<nomeSegmento>Farinha de Linha?/nomeSegmento>
I noticed that all special characters and the two subsequent are replaced by a single "?", causing the problem mentioned above.

Code (fw): Select all Collapse
****************************************************************************
METHOD Comunicar(cTipo, cFiltro) CLASS TAvantFiscal
****************************************************************************
*
* Comunicar no webservice
* Parametros: cTipo, cFiltro
* Retorno: lResult
*
* Autor: Samir
* 06/05/2015 - 14:16:50
*
****************************************************************************

local lResult := .T., cUrl := "", nContador := 0

   Default cTipo := ::cTipoFiltro

   //-- Limpar variável de retorno no inicio para caso gere erro não mantenha o anterior
   ::cRetorno := ""

   //-- Seta maiusculo para o tipo
   cTipo := UT(cTipo)

   //-- Preencher a URL do webservice
   cUrl := ::cUrl
   cUrl += "?idIntegracao=" + ::cIdIntegracao
   cUrl += "&idCliente=" + ::cIdCliente
   cUrl += "&token=" + ::cToken

   //-- Verificar o tipo do filtro
   If cTipo = "D" //-- Data

      Default cFiltro := DtoC(::dFiltro)

      cUrl += "&dtFiltro="+cFiltro

   ElseIf cTipo = "E" //-- Ean - Código de Barras

      Default cFiltro := ::cEAN

      cUrl += "&ean="+cFiltro

   ElseIf cTipo = "N" //-- NCM - Nomenclatura Comum Mercosul

      Default cFiltro := ::cNCM

      cUrl += "&ncm="+cFiltro

   Else

      Msg("Tipo de filtro inválido, verifique.")

      Return .F.

   End

   //-- Instanciar o objeto do Internet Explorer
   Try

      ::oConecta := CreateObject( "MSXML2.XMLHTTP.3.0" )

   Catch

      Try

         ::oConecta := CreateObject( "MSXML2.XMLHTTP" )

      Catch

         Try

            ::oConecta := CreateObject( "Microsoft.XMLHTTP" )

         catch

            Msg("Falha ao criar objeto de conexão."+ CRLF + "Verifique a versão do Internet Explorer e a conexão de internet.","Avant Fiscal")

            Return .F.

         End

      End

   End

   //HB_SetCodePage("PTISO") //-- Não resolveu

   //-- configura os parametros do wsdl
   ::oConecta:open("GET", cUrl, .F.)

   ::oConecta:SetRequestHeader( "Content-Type" , "text/xml; charset=utf-8" )
//   ::SetRequestHeader( "Content-Type" , "text/xml; charset=ISO-8859-1" ) //-- Não resolveu

   //-- envia o request
   TRY

      ::oConecta:send()

   CATCH oError

      Msg("Erro ao conectar ao site:;" + ::cUrl + CRLF + "Verifique a versão do Internet Explorer e a conexão de internet.","Avant Fiscal")

      Return .f.

   END

   //-- aguarda alguns segundos a resposta
   While ::oConecta:readyState <> 4

      millisec(50)

      SysRefresh()

      nContador++

      Barra(UT(nContador)+" de 100")

      //-- Teste para não permitir loop infinito
      If nContador > 100

         Msg("Tempo limite de conexão atingido, verifique.","Avant Fiscal")

         Return .F.

      End

   End

   //-- Gravar o retorno na variável
   ::cRetorno := ::oConecta:responsetext

   ::TratarRetorno()

   Msg(::cRetorno,cUrl)

   //HB_SetCodePage("EN") //-- Não precisa, pois a alteração acima não resolveu

return lResult
/*------------------------------------------------------------------------*/

****************************************************************************
METHOD TratarRetorno() CLASS TAvantFiscal
****************************************************************************
*
* Tratar retorno do site
* Parametros:
* Retorno:
*
* Autor: Samir
* 07/05/2015 - 16:39:02
*
****************************************************************************

local cFileRetorno := "", hHandle := 0, oXml
local aLinhas := {}, aResult := {}

//Portuguese      850            HB_CODEPAGE_PT850    "PT850"  cppt850.c
//Portuguese      ISO-8859-1     HB_CODEPAGE_PTISO    "PTISO"  cpptiso.c

   REQUEST HB_CODEPAGE_PT850
   REQUEST HB_CODEPAGE_PTISO

   aLinhas := HB_ATokens(cTexto,CRLF)
   aAdd(aResult,{"Normal",aLinhas[3275]} )

   If Sn("Testar 850")

      HB_SetCodePage( "PT850" )

      aAdd(aResult,{"PT850",aLinhas[3275]} )

      aAdd(aResult,{"PT850AnsitoOem",     HB_AnsiToOem(aLinhas[3275]) } )
      aAdd(aResult,{"PT850OemToAnsi",     HB_OemToAnsi(aLinhas[3275]) } )
      aAdd(aResult,{"PT850HB_STRTOUTF8",  HB_STRTOUTF8(aLinhas[3275]) } )
      aAdd(aResult,{"PT850HB_UTF8TOSTR",  HB_UTF8TOSTR(aLinhas[3275]) } )

        aAdd(aResult,{"PT850HB_Translate1", HB_Translate(aLinhas[3275],"PTISO","PT850") } )

      aAdd(aResult,{"PT850_AnsitoOem",    HB_AnsiToOem(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PT850_OemToAnsi",    HB_OemToAnsi(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PT850_HB_STRTOUTF8", HB_STRTOUTF8(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PT850_HB_UTF8TOSTR", HB_UTF8TOSTR(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )

      aAdd(aResult,{"PT850HB_Translate2", HB_Translate(aLinhas[3275],"PT850","PTISO") } )

      aAdd(aResult,{"PT850-AnsitoOem",    HB_AnsiToOem(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PT850-OemToAnsi",    HB_OemToAnsi(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PT850-HB_STRTOUTF8", HB_STRTOUTF8(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PT850-HB_UTF8TOSTR", HB_UTF8TOSTR(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      
   End

   If Sn("Testar ISO")

      HB_SetCodePage( "PTISO" )


      aAdd(aResult,{"PTISO",aLinhas[3275]} )

      aAdd(aResult,{"PTISOAnsitoOem",     HB_AnsiToOem(aLinhas[3275]) } )
      aAdd(aResult,{"PTISOOemToAnsi",     HB_OemToAnsi(aLinhas[3275]) } )
      aAdd(aResult,{"PTISOHB_STRTOUTF8",  HB_STRTOUTF8(aLinhas[3275]) } )
      aAdd(aResult,{"PTISOHB_UTF8TOSTR",  HB_UTF8TOSTR(aLinhas[3275]) } )
      
        aAdd(aResult,{"PTISOHB_Translate1", HB_Translate(aLinhas[3275],"PTISO","PT850") } )

      aAdd(aResult,{"PTISO_AnsitoOem",     HB_AnsiToOem(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PTISO_OemToAnsi",     HB_OemToAnsi(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PTISO_HB_STRTOUTF8",  HB_STRTOUTF8(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PTISO_HB_UTF8TOSTR",  HB_UTF8TOSTR(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )

      aAdd(aResult,{"PTISOHB_Translate2", HB_Translate(aLinhas[3275],"PT850","PTISO") } )

      aAdd(aResult,{"PTISO-AnsitoOem",     HB_AnsiToOem(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PTISO-OemToAnsi",     HB_OemToAnsi(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PTISO-HB_STRTOUTF8",  HB_STRTOUTF8(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      aAdd(aResult,{"PTISO-HB_UTF8TOSTR",  HB_UTF8TOSTR(HB_Translate(aLinhas[3275],"PTISO","PT850")) } )
      
   End
   
   aShow(aResult,"Testes")//-- Return below

   cFileRetorno := DirExe() + "RetornoAF_"+StrTran(Now(),":")+".xml"

   CreateTxt(cFileRetorno, HB_AnsiToOem(::cRetorno) )

   Msg("Arquivo criado")

   hHandle := FOpen( cFileRetorno )

   IF hHandle == -1

     Msg("Não foi possível abrir o XML!","Avant Fiscal")

   Else

      oXml := TXmlDocument():New( hHandle )

      IF oxml:nStatus != HBXML_STATUS_OK

         Msg("Erro ao processar o arquivo:" + CRLF +;
             "na Linha: " + AllTrim( Str( oxml:nLine ) ) + CRLF +;
             "Erro: " + HB_XmlErrorDesc( oxml:nError ) + CRLF +;
             "Erro na Tag: " + oxml:oErrorNode:cName + CRLF +;
             "Início Tag na linha: " + AllTrim( Str( oxml:oErrorNode:nBeginLine ) ),"Avant Fiscal")

      End

   End

   fClose( hHandle )

   fErase( cFileRetorno )

Return Nil

/*------------------------------------------------------------------------*/


Return from aShow

Normal, <nomeSegmento>Cacau em P?omeSegmento>
PT850, <nomeSegmento>Cacau em P?omeSegmento>
PT850AnsitoOem, <nomeSegmento>Cacau em P?omeSegmento>
PT850OemToAnsi, <nomeSegmento>Cacau em P?omeSegmento>
PT850HB_STRTOUTF8, <nomeSegmento>Cacau em P?omeSegmento>
PT850HB_UTF8TOSTR, <nomeSegmento>Cacau em P?omeSegmento>
PT850HB_Translate1, <nomeSegmento>Cacau em P?omeSegmento>
PT850_AnsitoOem, <nomeSegmento>Cacau em P?omeSegmento>
PT850_OemToAnsi, <nomeSegmento>Cacau em P?omeSegmento>
PT850_HB_STRTOUTF8, <nomeSegmento>Cacau em P?omeSegmento>
PT850_HB_UTF8TOSTR, <nomeSegmento>Cacau em P?omeSegmento>
PT850HB_Translate2, <nomeSegmento>Cacau em P?omeSegmento>
PT850-AnsitoOem, <nomeSegmento>Cacau em P?omeSegmento>
PT850-OemToAnsi, <nomeSegmento>Cacau em P?omeSegmento>
PT850-HB_STRTOUTF8, <nomeSegmento>Cacau em P?omeSegmento>
PT850-HB_UTF8TOSTR, <nomeSegmento>Cacau em P?omeSegmento>
PTISO, <nomeSegmento>Cacau em P?omeSegmento>
PTISOAnsitoOem, <nomeSegmento>Cacau em P?omeSegmento>
PTISOOemToAnsi, <nomeSegmento>Cacau em P?omeSegmento>
PTISOHB_STRTOUTF8, <nomeSegmento>Cacau em P?omeSegmento>
PTISOHB_UTF8TOSTR, <nomeSegmento>Cacau em P?omeSegmento>
PTISOHB_Translate1, <nomeSegmento>Cacau em P?omeSegmento>
PTISO_AnsitoOem, <nomeSegmento>Cacau em P?omeSegmento>
PTISO_OemToAnsi, <nomeSegmento>Cacau em P?omeSegmento>
PTISO_HB_STRTOUTF8, <nomeSegmento>Cacau em P?omeSegmento>
PTISO_HB_UTF8TOSTR, <nomeSegmento>Cacau em P?omeSegmento>
PTISOHB_Translate2, <nomeSegmento>Cacau em P?omeSegmento>
PTISO-AnsitoOem, <nomeSegmento>Cacau em P?omeSegmento>
PTISO-OemToAnsi, <nomeSegmento>Cacau em P?omeSegmento>
PTISO-HB_STRTOUTF8, <nomeSegmento>Cacau em P?omeSegmento>
PTISO-HB_UTF8TOSTR, <nomeSegmento>Cacau em P?omeSegmento>
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2

Continue the discussion