FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Help about xml files
Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Help about xml files
Posted: Mon Dec 27, 2010 03:38 PM

Hi,

How i can open a xml file and locate and get a data of tag <Xmotivo>

Xml

<?xml version="1.0" encoding="utf-8" ?>
- <Validacao>
<cStat>1</cStat>
<xMotivo>XML assinado e validado com sucesso.</xMotivo>
</Validacao>

Thanks in advance.

Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Mon Dec 27, 2010 06:37 PM
You can use a simplex function

Code (fw): Select all Collapse
MsgInfo( GetStringFromXMl('<xMotivo>','</xMotivo>', cXML ) )

Function GetStringFromXMl( cIni, cEnd, cString )
  Local nIni:=At( cIni, cString ) + Len( cIni )
  Local nFim:=At( cEnd, cString ) - nIni
Return Alltrim( Substr( cString, nIni, nEnd ) )


Or XML function:

demo.prg

Code (fw): Select all Collapse
#include "fivewin.ch"
#include "fileio.ch"
#include "hbxml.ch"

PROCEDURE Main()

public ownd
define window ownd title "XML File"

activate window ownd on init (Abrir_Xml("demo.xml"))

RETURN

Function Abrir_Xml(cFileName)

LOCAL hFile, cXml,xmlDoc, xmlIter, xmlNode, cNode, cAttrib, cValue, cData

hFile := FOpen( cFileName )

xmlDoc := TXmlDocument():New( hFile )
IF xmlDoc:nStatus != HBXML_STATUS_OK
MsgStop("Falha no arquivo XML ","ERRO Arquivo XML")
RETURN
ENDIF

xmlIter := TXmlIterator():New( xmlDoc:oRoot )
xmlNode := xmlIter:Find()

DO WHILE xmlNode != NIL

msginfo( xmlNode:cName ,"Field name")
msginfo( xmlNode:cData ,"VAlue from fieldr")

xmlNode := xmlIter:Next()

ENDDO

Return


Filename demo.xml

Code (fw): Select all Collapse
<?xml version="1.0" encoding="iso-8859-2"?>
<language lang="pt-br">
<Xml id="2.21">
      <data>26/12/2010</data>   
      <xharbour>xHarbour 1.2.1</xharbour>
      <fivewin>Fivewin 10.11</fivewin>
      <bcc>Borland 5.8.2</bcc>
      <Filexml>OK</Filexml>
</Xml>
</language>


:-)
Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Tue Dec 28, 2010 12:59 PM

Blz Lainton valeu pela dica, ainda estou um pouco confuso sobre os xmls, no caso no seu primeiro exemplo:

MsgInfo( GetStringFromXMl('<xMotivo>','</xMotivo>', cXML ) )

Function GetStringFromXMl( cIni, cEnd, cString )
Local nIni:=At( cIni, cString ) + Len( cIni )
Local nFim:=At( cEnd, cString ) - nIni
Return Alltrim( Substr( cString, nIni, nEnd ) )

Eu já teria que estar com com a variável cXML carregada com a linha correta, no caso a linha <xMotivo>....
Não existe uma forma sem ter que percorrer o xml todo dar por exemplo um find já na linha com a tag xMotivo e retornar o resultado da tag?

Obrigado.

Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Tue Dec 28, 2010 01:16 PM

você pode remover as quebras de linhas sendo assim sera apenas uma string
you can remove 'line break', and work only with string.

Sample

cXML := StrTran( MemoRead( "demo.xml" ), CRLF )

MsgInfo( GetStringFromXMl('<xMotivo>','</xMotivo>', cXML ) )

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Tue Dec 28, 2010 04:30 PM

Oi Lailton funcionou blz com algumas modificações, mas tem um problema é que no xml pode ocorrer de ter várias tags <xMotivo>, teria que ter como consultar uma determinada tag e fazer um laço para pegar as tags que estão dentro do íncio e fim desta tag, veja o xml:

  • <retConsReciCTe versao="1.03" xmlns="http://www.portalfiscal.inf.br/cte">
    <tpAmb>2</tpAmb>
    <verAplic>RS20100930124405</verAplic>
    <nRec>523000001374158</nRec>
    <cStat>104</cStat>
    <xMotivo>Lote processado</xMotivo>
    <cUF>52</cUF>
  • <protCTe versao="1.03">
  • <infProt Id="CTe352100000012092">
    <tpAmb>2</tpAmb>
    <verAplic>RS20100930124405</verAplic>
    <chCTe>52101204395298000137570010000050157641765209</chCTe>
    <dhRecbto>2010-12-28T14:18:03</dhRecbto>
    <nProt>352100000012092</nProt>
    <digVal>QziVmvLPT5TtkABP5K3IJiiyGRI=</digVal>
    <cStat>100</cStat>
    <xMotivo>Autorizado o uso do CT-e</xMotivo>
    </infProt>
    </protCTe>
    </retConsReciCTe>

Note que tem duas tags <xMotivo>. Estou trabalhando com o conhecimento eletrônico e já estou gerando o xml, validando, assinando, transmitindo e verificando o status de retorno agora preciso ler o xml todo de retorno para imprimir a DACTE, ficaria complicado ler ele todo para dentro de uma variável e ir fazendo testes de posições o correto é já dar um find em determinada tag, pegar o conteúdo e jogar em um dbf para ser utilizado com a fastreport.
Teria uma idéia de como fazer isso? Fico muitíssimo grato pela ajuda que vem me dando.

Obrigado.
Wanderson.

Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Tue Dec 28, 2010 07:09 PM
Bem acho que neste caso a melhor forma é vc quebrar por grupo o xml, concordo em relacao de 2 tag ou mais igual porem elas ficam dentro de outras tags,
For this way you can break by group, and find inside.
exemplo/sample:

demo.xml

Code (fw): Select all Collapse
<emit>
 <xNome>Lailton</xNome>
</emit>
<dest>
 <xNome>Wanderson</xNome>
</dest>


Prg

Code (fw): Select all Collapse
cXML := StrTran( MemoRead( "demo.xml" ), CRLF )

cGrupoEmit := GetStringFromXMl('<emit>','</emit>', cXML )
cGrupoDest := GetStringFromXMl('<dest>','</dest>', cXML )

? "Emit", GetStringFromXMl('<xNome>','</xNome>', cGrupoEmit )
? "Dest", GetStringFromXMl('<xNome>','</xNome>', cGrupoDest )


:-)
Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Tue Dec 28, 2010 09:21 PM

Hi Laiton, I change my mind cause i need just 2 information of xml file all the rest i have in dbf, very very more simple. Thanks again.

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Tue Jan 11, 2011 01:06 PM

Lailton yours tips are very usefull but in xml file xMotivo have a text:
Rejeição: CT-e ja esta cancelada na base de dados da SEFAZ
In msginfo with result variable the word Rejeição comes with many stranges caracteres i try to use OemToansi() but no success.

Thanks.

Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Tue Jan 11, 2011 02:16 PM

Can you provide a screenshot and post here ?

Thanks.

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Tue Jan 11, 2011 04:32 PM
lailton.webmaster wrote:Can you provide a screenshot and post here ?

Thanks.




Look at word Rejeição.

Thanks in advance.
Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Tue Jan 11, 2011 10:40 PM

Try:

Msginfo( HB_OemToAnsi( cString ) )

Msginfo( HB_AnsiToOem( cString ) )

If you can provide a prg to reproduce this i can test.

cya.

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Wed Jan 12, 2011 12:04 PM

Hi Lailton

Simple test:

Arq_Ret := "retorno.xml"
cXML := StrTran( MemoRead( Arq_Ret ), CRLF )
xMotivo := GetStringFromXMl('<xMotivo>','</xMotivo>', cXML )
MsgInfo(xMotivo)

retorno.xml:

<retCancCTe xmlns="http://www.portalfiscal.inf.br/cte" versao="1.03">
<infCanc Id="CTe52110104395298000137570010000000841589634677">
<tpAmb>2</tpAmb>
<cUF>52</cUF>
<verAplic>RS20100226092505</verAplic>
<cStat>218</cStat>
<xMotivo>Rejeição: CT-e ja esta cancelada na base de dados da SEFAZ</xMotivo>
<chCTe>52110104395298000137570010000000841589634677</chCTe>
<dhRecbto>2011-01-12T09:59:15</dhRecbto>
<nProt />
</infCanc>
</retCancCTe>

I try with hb_oemtoansi() and hb_ansitooem() and returns stranges caractres in word "Rejeição"

Thanks again.

Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Wed Jan 12, 2011 12:36 PM

It´s Utf8 you need convert, uf82ansi

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Help about xml files
Posted: Wed Jan 12, 2011 01:20 PM
lailton.webmaster wrote:It´s Utf8 you need convert, uf82ansi


Hi Lailton thanks for your help, I try to use xharbour function:

UTF8ToStr()

But the caracter Ç do not display correct.

http://img338.imageshack.us/i/rejeio.jpg/
Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Help about xml files
Posted: Wed Jan 12, 2011 01:57 PM
Try

Code (fw): Select all Collapse
HB_OemToAnsi( UTF8ToStr( cString ) )


Check it:
http://forums.fivetechsupport.com/viewtopic.php?f=6&t=10986&hilit=utf8