Jump to content
Fivewin Brasil

Trabalhar com texto em outro CodePage encoding="ISO-8859-1"


sambomb

Recommended Posts

Estou tentando acessar um webservice que trabalha com encoding="ISO-8859-1"

O problema é que o arquivo de retorno possui caracteres especiais e está gerando divergência nos dados.

Cabeçalho do XML

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

Trecho obtido do navegador:
<nomeSegmento>Cacau em Pó</nomeSegmento>

<nomeSegmento>Farinha de Linhaça</nomeSegmento>

Ao criar um TXT com o retorno do XML para tratar:
<nomeSegmento>Cacau em P?omeSegmento>
<nomeSegmento>Farinha de Linha?/nomeSegmento>

Notei que todos os caracteres especiais e os 2 subsequentes são substituidos por uma única interrogação, causando o problema citado acima

****************************************************************************
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" )
//   ::oConecta: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

//Bulgarian       Windows-1251   HB_CODEPAGE_BG1251   "BG1251" cpbgwin.c
//Bulgarian       MIK            HB_CODEPAGE_BGMIK    "BGMIK"  cpbgmik.c
//Croatien        1250           HB_CODEPAGE_HR1250   "HR1250" cphr1250.c
//Croatien        437            HB_CODEPAGE_HR437    "HR437"  cphr437.c
//Croatien        852            HB_CODEPAGE_HR852    "HR852"  cphr852.c
//English         437            none                 "EN"     cp_tpl.c
//French          850            HB_CODEPAGE_FR       "FR"     cpfrdos.c
//German          850            HB_CODEPAGE_DE       "DE"     cpgedos.c
//German          ISO-8859-1     HB_CODEPAGE_DEWIN    "DEWIN"  cpgewin.c
//Greek (Dos)     737            HB_CODEPAGE_EL       "EL"     cpeldos.c
//Greek WIN       ANSI (1253)    HB_CODEPAGE_ELWIN    "ELWIN"  cpelwin.c
//Hungarian       852            HB_CODEPAGE_HU852    "HU852"  cphu852.c
//Hungarian       Windows-1250   HB_CODEPAGE_HUWIN    "HUWIN"  cphuwin.c
//Italian         437            HB_CODEPAGE_IT437    "IT437"  cpit437.c
//Italian         850            HB_CODEPAGE_IT850    "IT850"  cpit850.c
//Italian         ISO-8859-1     HB_CODEPAGE_ITISO    "ITISO"  cpitiso.c
//Italian         ISO-8859-1b    HB_CODEPAGE_ITISB    "ITISB"  cpitisb.c (with BOX chars)
//Lithuanian      Windows-1257   HB_CODEPAGE_LT       "LT"     cpltwin.c
//Polish          852            HB_CODEPAGE_PL852    "PL852"  cppl852.c
//Polish          ISO-8859-2     HB_CODEPAGE_PLISO    "PLISO"  cppliso.c
//Polish          Mazovia        HB_CODEPAGE_PLMAZ    "PLMAZ"  cpplmaz.c
//Polish          Windows-1250   HB_CODEPAGE_PLWIN    "PLWIN"  cpplwin.c

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

//Russian         Windows-1251   HB_CODEPAGE_RU1251   "RU1251" cpruwin.c
//Russian         866            HB_CODEPAGE_RU866    "RU866"  cpru866.c
//Russian         KOI-8          HB_CODEPAGE_RUKOI8   "RUKOI8" cprukoi.c
//Serbian         Windows-1251   HB_CODEPAGE_SRWIN    "SRWIN"  cpsrwin.c
//Slovenian       852            HB_CODEPAGE_SL852    "SL852"  cpsl852.c
//Slovenian       ISO-8859-2     HB_CODEPAGE_SLISO    "SLISO"  cpsliso.c
//Slovenian       Windows-1250   HB_CODEPAGE_SLWIN    "SLWIN"  cpslwin.c
//Spanish         850            HB_CODEPAGE_ES       "ES"     cpesdos.c
//Spanish(Modern) ISO-8859-1     HB_CODEPAGE_ESMWIN   "ESMWIN" cpesmwin.c
//Spanish         ISO-8859-1     HB_CODEPAGE_ESWIN    "ESWIN"  cpeswin.c
//Ukrainian       Windows-1251   HB_CODEPAGE_UA1251   "UA1251" cpuawin.c
//Ukrainian       866            HB_CODEPAGE_UA866    "UA866"  cpua866.c
//Ukrainian       KOI-8U         HB_CODEPAGE_UAKOI8   "UAKOI8" cpuakoi.c

//-- Nenhum desses tratamentos resolveu
//   HB_SetCodePage("PTISO")
//   Debuga HB_LangName()

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

//   CreateTxt(cFileRetorno, ::cRetorno )

//   HB_SetCodePage("PT850")
//   Debuga HB_LangName()

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

//   CreateTxt(cFileRetorno, ::cRetorno )

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

//   CreateTxt(cFileRetorno, HB_Translate(::cRetorno,"PT850","EN") )

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

//   CreateTxt(cFileRetorno, OemToAnsi(::cRetorno ) )

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

//   CreateTxt(cFileRetorno, AnsitoOem(::cRetorno ) )

   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
/*------------------------------------------------------------------------*/
Link to comment
Share on other sites

****************************************************************************
static procedure TestaEncode(cTexto)
****************************************************************************
*
* testar codificacao
* Parametros: cTexto
* Retorno: Nenhum
*
* Autor: Samir
* 08/05/2015 - 14:42:50
*
****************************************************************************

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 Nil

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

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>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...