Help about xml files

Help about xml files

Postby Wanderson » Mon Dec 27, 2010 3: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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » Mon Dec 27, 2010 6:37 pm

You can use a simplex function

Code: Select all  Expand view

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: Select all  Expand view
#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: Select all  Expand view
<?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>
 


:)
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Help about xml files

Postby Wanderson » 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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » Tue Dec 28, 2010 1: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 ) )
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Help about xml files

Postby Wanderson » Tue Dec 28, 2010 4: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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » Tue Dec 28, 2010 7: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: Select all  Expand view
<emit>
 <xNome>Lailton</xNome>
</emit>
<dest>
 <xNome>Wanderson</xNome>
</dest>


Prg

Code: Select all  Expand view
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 )


:wink:
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Help about xml files

Postby Wanderson » Tue Dec 28, 2010 9: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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby Wanderson » Tue Jan 11, 2011 1: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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » Tue Jan 11, 2011 2:16 pm

Can you provide a screenshot and post here ?

Thanks.
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Help about xml files

Postby Wanderson » Tue Jan 11, 2011 4:32 pm

lailton.webmaster wrote:Can you provide a screenshot and post here ?

Thanks.


Image

Look at word Rejeição.

Thanks in advance.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » 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.
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Help about xml files

Postby Wanderson » 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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » Wed Jan 12, 2011 12:36 pm

It´s Utf8 you need convert, uf82ansi
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Help about xml files

Postby Wanderson » Wed Jan 12, 2011 1: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/
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Help about xml files

Postby lailton.webmaster » Wed Jan 12, 2011 1:57 pm

lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 84 guests