FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Lector de XML
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Lector de XML
Posted: Wed Feb 27, 2013 07:19 AM
Este es un ejemplo sencillo de como analizar un XML:

Si sabes como mejorarlo, se agradece tu colaboración para que asi tengamos una función de lectura genérica de un XML :-)

xmlreader.prg
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()
   
   local hFile    := FOpen( "test.xml" ) 
   Local oMnuXml  := TXmlDocument():New( hFile )
   Local oXmlNode := oMnuXml:FindFirst( "Header" )
   Local oXmlIter := TXmlIterator():New( oXmlNode ), oTagActual, cAttribute

   while .T.
      oTagActual = oXmlIter:Next()
      If oTagActual != nil
         ? oTagActual:cName, oTagActual:cData
      Else
         Exit
      Endif
   End

   oXmlNode = oMnuXml:FindFirst( "Detail" )
   oXmlIter = TXmlIterator():New( oXmlNode )

   while .T.
      oTagActual = oXmlIter:Next()
      If oTagActual != nil
         MsgInfo(  oTagActual:cName, oTagActual:cData )
         HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )
         // MsgInfo( ValType( oTagActual:aAttributes ) )
         // for each cAttribute in oTagActual:aAttributes
         //    MsgInfo( cAttribute, oTagActual:GetAttribute( cAttribute ) )
         // next   
      Else
         Exit
      Endif
   End

   FClose( hFile )

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Lector de XML
Posted: Wed Feb 27, 2013 12:02 PM
Esta versión es más simple y debiera funcionar bien con cualquier XML :-)

xmlreader.prg
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()
   
   local hFile    := FOpen( "test.xml" ) 
   Local oXmlDoc  := TXmlDocument():New( hFile )
   Local oXmlIter := TXmlIterator():New( oXmlDoc:oRoot ), oTagActual

   while .T.
      oTagActual = oXmlIter:Next()
      If oTagActual != nil
         MsgInfo( oTagActual:cName, oTagActual:cData )
         HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )
      Else
         Exit
      Endif
   End

   FClose( hFile )

return nil


test.xml
Code (fw): Select all Collapse
<xml>
<one>
<name>FiveWin</name>
<company>FiveTech Software</company>
</one>
<two attribute1="att1" attribute2="att2">
</two>
<three>
</three>
</xml>
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 205
Joined: Fri Oct 07, 2005 05:07 PM
Re: Lector de XML
Posted: Wed Feb 27, 2013 12:56 PM
Buenas
con la version de harbour (v.3.2 rev 18563), tengo que hacer estos cambios y funciona bien
Code (fw): Select all Collapse
#include "FiveWin.ch"                                                          
                                                                               
function Main()                                                                
                                                                               
   local hFile    := FOpen( "rai102.xml" )                                       
   Local oXmlDoc  := TXmlDocument():New( hFile )                               
   Local oXmlIter := TXmlIterator():New( oXmlDoc:oRoot ), oTagActual           
                                                                               
   while .T.                                                                   
      oTagActual = oXmlIter:Next()                                             
      If oTagActual != nil                                                     
         MsgInfo( oTagActual:cName, oTagActual:cData )   
         If Len( oTagActual:aAttributes ) > 0
         Eval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )                                                                   
         endIf
      Else                                                                     
         Exit                                                                  
      Endif                                                                    
   End                                                                         
                                                                               
   FClose( hFile )                                                             
                                                                               
return nil
Saludos/regards

RenOmaS



skype: americo.balboa
Posts: 8523
Joined: Tue Dec 20, 2005 07:36 PM
Re: Lector de XML
Posted: Wed Feb 27, 2013 12:58 PM
También se puede hacer asi:

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

FUNCTION Main()

   LOCAL hFile, cXml, cFileName:="TEST.XML"
   LOCAL xmlDoc, xmlIter , xmlNode, cNode, cAttrib, cValue, oCampo, oConteudo

   IF .NOT. FILE( cFileName )

      MsgInfo( "Arquivo Não Encontrado !!!","Atenção" ) // nome do arquivo

      cFileName := cGetFile( "XML File (*.Xml)|*.Xml|","Selecione arquivo XML da NFe ",curdir())

      IF .NOT. FILE( cFileName )

         RETURN NIL

      ENDIF

   ENDIF

   cNode   := NIL
   cAttrib := NIL
   cValue  := NIL
   hFile  := FOpen( cFileName )

   xmlDoc := TXmlDocument():New( hFile )

   IF xmlDoc:nStatus != HBXML_STATUS_OK

      Msginfo( "Erro ao Ler Arquivo .XML" )

      RETURN NIL

   ENDIF

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

   DO WHILE xmlNode != NIL

      SYSREFRESH()

      IF .NOT. EMPTY( xmlNode:cData )

         IF SUBS( xmlNode:cData,1,1 ) # "<"

            oCampo    := xmlNode:cName

            oConteudo := xmlNode:cData

         ENDIF

      ELSE

        oCampo := xmlNode:cName

      ENDIF

      ? oCampo, oConteudo

      xmlNode := xmlIter:Next() // joga pro proximo campo

   ENDDO

RETURN NIL

// FIN
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 8523
Joined: Tue Dec 20, 2005 07:36 PM
Re: Lector de XML
Posted: Mon Nov 16, 2015 11:48 AM

Eduardo Arena, que error te dá? Muestra como estás haciendo ó poste el .XML. Gracias,

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341

Continue the discussion