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.
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.
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 ) )#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<?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>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.
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 ) )
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:
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.
<emit>
<xNome>Lailton</xNome>
</emit>
<dest>
<xNome>Wanderson</xNome>
</dest>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 )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.
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.
Can you provide a screenshot and post here ?
Thanks.
Try:
Msginfo( HB_OemToAnsi( cString ) )
Msginfo( HB_AnsiToOem( cString ) )
If you can provide a prg to reproduce this i can test.
cya.
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.
It´s Utf8 you need convert, uf82ansi
lailton.webmaster wrote:It´s Utf8 you need convert, uf82ansi
HB_OemToAnsi( UTF8ToStr( cString ) )