Lector de XML

Lector de XML

Postby Antonio Linares » Wed Feb 27, 2013 7: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: Select all  Expand view
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 41898
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Lector de XML

Postby Antonio Linares » Wed Feb 27, 2013 12:02 pm

Esta versión es más simple y debiera funcionar bien con cualquier XML :-)

xmlreader.prg
Code: Select all  Expand view
#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: Select all  Expand view
<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
User avatar
Antonio Linares
Site Admin
 
Posts: 41898
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Lector de XML

Postby RenOmaS » 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: Select all  Expand view
#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
User avatar
RenOmaS
 
Posts: 205
Joined: Fri Oct 07, 2005 5:07 pm

Re: Lector de XML

Postby karinha » Wed Feb 27, 2013 12:58 pm

También se puede hacer asi:

Code: Select all  Expand view

#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
User avatar
karinha
 
Posts: 7641
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Lector de XML

Postby karinha » 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
User avatar
karinha
 
Posts: 7641
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: JoseAlvarez and 48 guests