Visor de XML

Visor de XML

Postby Antonio Linares » Wed Feb 27, 2013 3:49 pm

Ahora que hemos publicado una forma simple de analizar un fichero XML, ahora podemos implementarle un visor GUI:

viewtopic.php?f=3&t=25741

xmltree.prg
Code: Select all  Expand view
#include "FiveWin.ch"
#include "Splitter.ch"

static oSplit1, oSplit2, oLbxDatas, oLbxMethods

//----------------------------------------------------------------------------//

function Main()

   local oWnd

   DEFINE WINDOW oWnd TITLE "XML viewer" ;
      MENU BuildMenu()

   ACTIVATE WINDOW oWnd ;
      ON INIT BuildTree( oWnd ) ;
      ON RESIZE ( If( oSplit1 != nil, oSplit1:AdjLeft(),),;
                  If( oSplit2 != nil, oSplit2:AdjRight(),) )

return nil

//----------------------------------------------------------------------------//

function BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "About" ACTION MsgAbout( "XML Viewer", "FiveTech Software" )
   ENDMENU

return oMenu

//----------------------------------------------------------------------------//

function BuildTree( oWnd )

   local oTree := TTreeView():New( 0, 0, oWnd )
   local oClass, cData, cMethod
   local hFile, oXmlDoc, oXmlIter, oTagActual
   local oTagLast, aRoots := {}

   oTree:nWidth = 180
   // oTree:SetImageList( oImageList )

   oTree:Expand()

   @ 0, 186 LISTBOX oLbxDatas VAR cData ITEMS { "one", "two", "three" } ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 391 LISTBOX oLbxMethods VAR cMethod ITEMS { "one", "two", "three" } ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 181 SPLITTER oSplit1 ;
      VERTICAL ;
      PREVIOUS CONTROLS oTree ;
      HINDS CONTROLS oLbxDatas ;
      LEFT MARGIN 150 ;  
      RIGHT MARGIN oSplit2:nLast + 100 ;
      SIZE 4, 300  PIXEL ;
      OF oWnd STYLE

   @ 0, 386 SPLITTER oSplit2 ;
      VERTICAL ;
      PREVIOUS CONTROLS oLbxDatas ;
      HINDS CONTROLS oLbxMethods ;
      LEFT MARGIN oSplit1:nFirst + 120 ;
      RIGHT MARGIN 80 ;
      SIZE 4, 300 PIXEL ;
      OF oWnd STYLE

   hFile    = FOpen( "test.xml" )
   oXmlDoc  = TXmlDocument():New( hFile )
   oXmlIter = TXmlIterator():New( oXmlDoc:oRoot )

   AAdd( aRoots, oTree )

   while ( oTagActual := oXmlIter:Next() ) != nil
      // aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
      // oRoot:Add( oTagActual:cName )
      if oTagLast != nil
         if oTagLast:Depth() < oTagActual:Depth()
            ASize( aRoots, Len( aRoots ) + 1 )
            aRoots[ oTagActual:Depth() ] = aRoots[ oTagActual:Depth() - 1 ]:Add( oTagActual:cName )
            // MsgInfo( oTagActual:cName + ", " + "open node", oTagActual:Depth() )
         endif
         if oTagLast:Depth() > oTagActual:Depth()
            aRoots[ oTagActual:Depth() - 1 ]:Add( oTagActual:cName )
            // MsgInfo( oTagActual:cName + ", " + "close node", oTagActual:Depth() )
         endif
         if oTagLast:Depth() == oTagActual:Depth()
            aRoots[ Max( oTagLast:Depth() - 1, 1 ) ]:Add( oTagActual:cName )
            // MsgInfo( oTagActual:cName + ", " + "keeps node", oTagActual:Depth() )
         endif
      else
         AAdd( aRoots, oTree:Add( oTagActual:cName ) )
         // MsgInfo( oTagActual:cName + ", " + "starts", oTagActual:Depth() )  
      endif
      oTagLast = oTagActual
   end  
 
   // HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )

   FClose( hFile )

   // oTree:bChanged = { || ShowClassInfo( oTree ) }

return nil

//----------------------------------------------------------------------------//


Image

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: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby RenOmaS » Wed Feb 27, 2013 10:02 pm

Buenas,
Creo que asi queda mejor la estructura

Code: Select all  Expand view

#include "FiveWin.ch"
#include "Splitter.ch"

static oSplit1, oSplit2, oLbxDatas, oLbxMethods

//----------------------------------------------------------------------------/

function Test()

   local oWnd

   DEFINE WINDOW oWnd TITLE "XML viewer" ;
      MENU BuildMenu()

   ACTIVATE WINDOW oWnd ;
      ON INIT BuildTree( oWnd ) ;
      ON RESIZE ( If( oSplit1 != nil, oSplit1:AdjLeft(),),;
                  If( oSplit2 != nil, oSplit2:AdjRight(),) )

return nil

//----------------------------------------------------------------------------/


function BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "About" ACTION MsgAbout( "XML Viewer", "FiveTech Software" )
   ENDMENU

return oMenu

//----------------------------------------------------------------------------/


function BuildTree( oWnd )

   local oTree := TTreeView():New( 0, 0, oWnd )
   local oClass, cData, cMethod
   local hFile, oXmlDoc, oXmlIter, oTagActual
   local oTagLast, aRoots := {}

   oTree:nWidth = 180
   // oTree:SetImageList( oImageList )

   oTree:Expand()

   @ 0, 186 LISTBOX oLbxDatas VAR cData ITEMS { "one", "two", "three" } ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 391 LISTBOX oLbxMethods VAR cMethod ITEMS { "one", "two", "three" } ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 181 SPLITTER oSplit1 ;
      VERTICAL ;
      PREVIOUS CONTROLS oTree ;
      HINDS CONTROLS oLbxDatas ;
      LEFT MARGIN 150 ;
      RIGHT MARGIN oSplit2:nLast + 100 ;
      SIZE 4, 300  PIXEL ;
      OF oWnd STYLE

   @ 0, 386 SPLITTER oSplit2 ;
      VERTICAL ;
      PREVIOUS CONTROLS oLbxDatas ;
      HINDS CONTROLS oLbxMethods ;
      LEFT MARGIN oSplit1:nFirst + 120 ;
      RIGHT MARGIN 80 ;
      SIZE 4, 300 PIXEL ;
      OF oWnd STYLE

   hFile    = FOpen( "test.xml" )
   oXmlDoc  = TXmlDocument():New( hFile )
   oXmlIter = TXmlIterator():New( oXmlDoc:oRoot )

   AAdd( aRoots, oTree )

   while ( oTagActual := oXmlIter:Next() ) != nil
      // aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
      // oRoot:Add( oTagActual:cName )
      if oTagLast != nil
         if oTagLast:Depth() < oTagActual:Depth()
            ASize( aRoots, Len( aRoots ) + 1 )
            aRoots[ oTagActual:Depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )

         endif
         if oTagLast:Depth() > oTagActual:Depth()
            aRoots[ oTagActual:depth() + 1 ] := aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
         endif
         if oTagLast:Depth() == oTagActual:Depth()
            aRoots[ Max( oTagLast:Depth(), 1 ) ]:Add( oTagActual:cName )
         endif
      else
         AAdd( aRoots, oTree:Add( oTagActual:cName ) )

      endif
      oTagLast = oTagActual
   end

   // HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue )} )

   FClose( hFile )

   // oTree:bChanged = { || ShowClassInfo( oTree ) }

return nil

//----------------------------------------------------------------------------/
 


Image
Saludos/regards
RenOmaS

skype: americo.balboa
User avatar
RenOmaS
 
Posts: 205
Joined: Fri Oct 07, 2005 5:07 pm

Re: Visor de XML

Postby Antonio Linares » Wed Feb 27, 2013 10:23 pm

gracias! :-)

Creo que aún falla en ramas hijas, a ver si conseguimos arreglarlo :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby RenOmaS » Wed Feb 27, 2013 10:29 pm

Aca me funciona bien

Image

son xml's simples..
Saludos/regards
RenOmaS

skype: americo.balboa
User avatar
RenOmaS
 
Posts: 205
Joined: Fri Oct 07, 2005 5:07 pm

Re: Visor de XML

Postby Antonio Linares » Wed Feb 27, 2013 10:57 pm

Si, cierto, está ya funcionando genial :-) Muchas gracias!

Ahora lo suyo seria que en el lado derecho mostremos el valor de cada item...
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby Antonio Linares » Wed Feb 27, 2013 11:39 pm

Esta versión ya muestra los valores de los items a la derecha :-)

xmltree.prg
Code: Select all  Expand view
#include "FiveWin.ch"
#include "Splitter.ch"

static oSplit1, oSplit2, oLbxDatas, oLbxMethods

//----------------------------------------------------------------------------/

function Test()

   local oWnd

   DEFINE WINDOW oWnd TITLE "XML viewer" ;
      MENU BuildMenu()

   ACTIVATE WINDOW oWnd ;
      ON INIT BuildTree( oWnd ) ;
      ON RESIZE ( If( oSplit1 != nil, oSplit1:AdjLeft(),),;
                  If( oSplit2 != nil, oSplit2:AdjRight(),) )

return nil

//----------------------------------------------------------------------------/


function BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "About" ACTION MsgAbout( "XML Viewer", "(c) FiveTech Software 2013" )
   ENDMENU

return oMenu

//----------------------------------------------------------------------------/


function BuildTree( oWnd )

   local oTree := TTreeView():New( 0, 0, oWnd )
   local oClass, cData, cMethod
   local hFile, oXmlDoc, oXmlIter, oTagActual
   local oTagLast, aRoots := {}

   oTree:nWidth = 180
   // oTree:SetImageList( oImageList )

   oTree:Expand()

   @ 0, 186 LISTBOX oLbxDatas VAR cData ITEMS {} ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 391 LISTBOX oLbxMethods VAR cMethod ITEMS {} ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 181 SPLITTER oSplit1 ;
      VERTICAL ;
      PREVIOUS CONTROLS oTree ;
      HINDS CONTROLS oLbxDatas ;
      LEFT MARGIN 150 ;
      RIGHT MARGIN oSplit2:nLast + 100 ;
      SIZE 4, 300  PIXEL ;
      OF oWnd STYLE

   @ 0, 386 SPLITTER oSplit2 ;
      VERTICAL ;
      PREVIOUS CONTROLS oLbxDatas ;
      HINDS CONTROLS oLbxMethods ;
      LEFT MARGIN oSplit1:nFirst + 120 ;
      RIGHT MARGIN 80 ;
      SIZE 4, 300 PIXEL ;
      OF oWnd STYLE

   hFile    = FOpen( "test.xml" )
   oXmlDoc  = TXmlDocument():New( hFile )
   oXmlIter = TXmlIterator():New( oXmlDoc:oRoot )

   AAdd( aRoots, oTree )

   while ( oTagActual := oXmlIter:Next() ) != nil
      if oTagLast != nil
         if oTagLast:Depth() < oTagActual:Depth()
            ASize( aRoots, Len( aRoots ) + 1 )
            aRoots[ oTagActual:Depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
            aRoots[ oTagActual:Depth() + 1 ]:Cargo = oTagActual:cData
         endif

         if oTagLast:Depth() > oTagActual:Depth()
            aRoots[ oTagActual:depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
            aRoots[ oTagActual:depth() + 1 ]:Cargo = oTagActual:cData
         endif

         if oTagLast:Depth() == oTagActual:Depth()
            aRoots[ Max( oTagLast:Depth(), 1 ) ]:Add( oTagActual:cName ):Cargo = oTagActual:cData
         endif
      else
         AAdd( aRoots, oTree:Add( oTagActual:cName ) )
         ATail( aRoots ):Cargo = oTagActual:cData
      endif
      oTagLast = oTagActual
   end

   // HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue )} )

   FClose( hFile )

   oTree:bChanged = { | oItem | oLbxDatas:SetItems( { oItem:GetSelected():Cargo } ) }

return nil

//----------------------------------------------------------------------------/


Image

test.xml
Code: Select all  Expand view
<xml>
<one>
<name>FiveWin</name>
<company>FiveTech Software
<country>Spain</country>
<region>EU</region>
</company>
</one>
<two attribute1="att1" attribute2="att2">
</two>
<three>
<first>any</first>
<second>value</second>
</three>
</xml>
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby Antonio Linares » Wed Feb 27, 2013 11:57 pm

Y esta versión ya muestra los atributos de cada item en el listbox de más a la derecha :-)

xmltree.prg
Code: Select all  Expand view
#include "FiveWin.ch"
#include "Splitter.ch"

static oSplit1, oSplit2, oLbxDatas, oLbxMethods

//----------------------------------------------------------------------------/

function Test()

   local oWnd

   DEFINE WINDOW oWnd TITLE "XML viewer" ;
      MENU BuildMenu()

   ACTIVATE WINDOW oWnd ;
      ON INIT BuildTree( oWnd ) ;
      ON RESIZE ( If( oSplit1 != nil, oSplit1:AdjLeft(),),;
                  If( oSplit2 != nil, oSplit2:AdjRight(),) )

return nil

//----------------------------------------------------------------------------/


function BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "About" ACTION MsgAbout( "XML Viewer", "(c) FiveTech Software 2013" )
   ENDMENU

return oMenu

//----------------------------------------------------------------------------/


function BuildTree( oWnd )

   local oTree := TTreeView():New( 0, 0, oWnd )
   local oClass, cData, cMethod
   local hFile, oXmlDoc, oXmlIter, oTagActual
   local oTagLast, aRoots := {}

   oTree:nWidth = 180
   // oTree:SetImageList( oImageList )

   oTree:Expand()

   @ 0, 186 LISTBOX oLbxDatas VAR cData ITEMS {} ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 391 LISTBOX oLbxMethods VAR cMethod ITEMS {} ;
      SIZE 200, 200 PIXEL OF oWnd

   @ 0, 181 SPLITTER oSplit1 ;
      VERTICAL ;
      PREVIOUS CONTROLS oTree ;
      HINDS CONTROLS oLbxDatas ;
      LEFT MARGIN 150 ;
      RIGHT MARGIN oSplit2:nLast + 100 ;
      SIZE 4, 300  PIXEL ;
      OF oWnd STYLE

   @ 0, 386 SPLITTER oSplit2 ;
      VERTICAL ;
      PREVIOUS CONTROLS oLbxDatas ;
      HINDS CONTROLS oLbxMethods ;
      LEFT MARGIN oSplit1:nFirst + 120 ;
      RIGHT MARGIN 80 ;
      SIZE 4, 300 PIXEL ;
      OF oWnd STYLE

   hFile    = FOpen( "test.xml" )
   oXmlDoc  = TXmlDocument():New( hFile )
   oXmlIter = TXmlIterator():New( oXmlDoc:oRoot )

   AAdd( aRoots, oTree )

   while ( oTagActual := oXmlIter:Next() ) != nil
      if oTagLast != nil
         if oTagLast:Depth() < oTagActual:Depth()
            ASize( aRoots, Len( aRoots ) + 1 )
            aRoots[ oTagActual:Depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
            aRoots[ oTagActual:Depth() + 1 ]:Cargo = oTagActual
         endif

         if oTagLast:Depth() > oTagActual:Depth()
            aRoots[ oTagActual:depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
            aRoots[ oTagActual:depth() + 1 ]:Cargo = oTagActual
         endif

         if oTagLast:Depth() == oTagActual:Depth()
            aRoots[ Max( oTagLast:Depth(), 1 ) ]:Add( oTagActual:cName ):Cargo = oTagActual
         endif
      else
         AAdd( aRoots, oTree:Add( oTagActual:cName ) )
         ATail( aRoots ):Cargo = oTagActual
      endif
      oTagLast = oTagActual
   end

   FClose( hFile )

   oTree:bChanged = { | oItem | oLbxDatas:SetItems( { oItem:GetSelected():Cargo:cData } ),;
                                oLbxMethods:Reset(),;
                                HEval( oItem:GetSelected():Cargo:aAttributes,;
                                { | cKey, cData | oLbxMethods:Add( cKey + " : " + cData ) } ) }

return nil

//----------------------------------------------------------------------------/


Image

test.xml
Code: Select all  Expand view
<xml>
<one>
<name>FiveWin</name>
<company>FiveTech Software
<country>Spain</country>
<region>EU</region>
</company>
</one>
<two attribute1="att1" attribute2="att2">
</two>
<three>
<first>any</first>
<second>value</second>
</three>
</xml>
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby Antonio Linares » Thu Feb 28, 2013 12:27 am

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby Antonio Linares » Thu Feb 28, 2013 4:23 pm

Esta modificación puede ser necesaria en la clase TXmlDocument de Harbour, según el xml inspeccionado:

METHOD New( xElem, nStyle ) CLASS TXMLDocument
Code: Select all  Expand view
        IF ! Empty( ::oRoot:oChild ) .AND. ::oRoot:oChild:cName == "xml" .and. ::oRoot:oChild:cData != nil
            ::cHeader := "<=xml " + ::oRoot:oChild:cData + "?>"
         ENDIF
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby RenOmaS » Thu Feb 28, 2013 7:51 pm

Tambem que no me quiere tomar es algunos xml's que tengan atributos asi: Font.Name="Arial",
NO se si sea e formato correcto de un fichero xml.

Code: Select all  Expand view
<TfrxMemoView Name="System" Align="baBottom" Left="-26,45671" Top="480,000676141732" Width="18,89765" Height="566,929133858268" ShowHint="False" AutoWidth="True" Font.Charset="1" Font.Color="-16777208" Font.Height="-11" Font.Name="Arial"  />


Estoy a querer leer fichero de fastreport
Saludos/regards
RenOmaS

skype: americo.balboa
User avatar
RenOmaS
 
Posts: 205
Joined: Fri Oct 07, 2005 5:07 pm

Re: Visor de XML

Postby Antonio Linares » Thu Feb 28, 2013 8:36 pm

Americo, (?)

Puesto que usamos las funciones de XML de Harbour y xHarbour (contrib/xhb/txml.prg), si eres tan amable podrias crear un pequeño ejemplo que no use FWH y que demuestre el problema con los puntos y preguntarlo en la lista de desarrollo de Harbour:

https://groups.google.com/forum/?fromgroups#!forum/harbour-devel

gracias
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Visor de XML

Postby RenOmaS » Fri Mar 01, 2013 12:56 pm

Américo, (?)

Si.

Ya coloque en el forum indicado

Gracias.
Saludos/regards
RenOmaS

skype: americo.balboa
User avatar
RenOmaS
 
Posts: 205
Joined: Fri Oct 07, 2005 5:07 pm


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 36 guests