XML reader

XML reader

Postby Antonio Linares » Wed Feb 27, 2013 7:16 am

This is a simple XML analizer example

If you know how to improve it, lets improve it to get a generic XML reader function :-)

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

Re: XML reader

Postby Silvio.Falconi » Wed Feb 27, 2013 8:08 am

Antonio,
just to try it , these classes are in fwh, xharbour or harbour?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6772
Joined: Thu Oct 18, 2012 7:17 pm

Re: XML reader

Postby Antonio Linares » Wed Feb 27, 2013 8:27 am

Silvio,

They are available in both Harbour and xHarbour
regards, saludos

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

Re: XML reader

Postby Silvio.Falconi » Wed Feb 27, 2013 9:09 am

Sorry Antonio,
I tried with a test.xml I created from my application ( from a dbf)
and it make an error I not Know why


Code: Select all  Expand view


Application
===========
   Path and name: C:\work\errori\xmlreader\test.Exe (32 bits)
   Size: 1,803,264 bytes
   Compiler version: xHarbour build 1.2.1 Intl. (SimpLex) (Rev. 6715)
   FiveWin  Version: FWHX 12.03
   Windows version: 5.1, Build 2600 Service Pack 2

   Time from start: 0 hours 0 mins 0 secs
   Error occurred at: 02/27/13, 10:06:44
   Error description: Error BASE/1004  Class: 'NIL' has no exported method: DEPTH
   Args:
     [   1] = U  

Stack Calls
===========
   Called from:  => DEPTH( 0 )
   Called from: source\rtl\txml.prg => TXMLITERATOR:NEW( 257 )
   Called from: test.prg => MAIN( 8 )
 




my test.xml

Code: Select all  Expand view


<?xml version="1.0" encoding="ISO-8859-1" ?>
<ROOT DATABASE="clienti.dbf">
  <Structure>
  <Field>
  <Field_name>CLNOMBRE</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        40</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLCIF</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        15</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLCATEGOR</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        40</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLNOTAS</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>       255</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLDIRECC</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        50</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLLOCALI</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        50</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLPAIS</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        30</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLTELEFONO</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        15</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLMOVIL</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        15</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLFAX</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        15</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLCONTACTO</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        40</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLEMAIL</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        50</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  <Field>
  <Field_name>CLURL</Field_name>
  <Field_type>Character</Field_type>
  <Field_length>        50</Field_length>
  <Field_decimals>         0</Field_decimals>
  </Field>
  </Structure>
  <Data>
  <Record>
    <CLNOMBRE>Clientes varios</CLNOMBRE>
    <CLCIF></CLCIF>
    <CLCATEGOR></CLCATEGOR>
    <CLNOTAS>m</CLNOTAS>
    <CLDIRECC></CLDIRECC>
    <CLLOCALI></CLLOCALI>
    <CLPAIS></CLPAIS>
    <CLTELEFONO></CLTELEFONO>
    <CLMOVIL></CLMOVIL>
    <CLFAX></CLFAX>
    <CLCONTACTO></CLCONTACTO>
    <CLEMAIL>l</CLEMAIL>
    <CLURL></CLURL>
  </Record>
  <Record>
    <CLNOMBRE>Empresa 1</CLNOMBRE>
    <CLCIF></CLCIF>
    <CLCATEGOR></CLCATEGOR>
    <CLNOTAS></CLNOTAS>
    <CLDIRECC></CLDIRECC>
    <CLLOCALI></CLLOCALI>
    <CLPAIS></CLPAIS>
    <CLTELEFONO></CLTELEFONO>
    <CLMOVIL></CLMOVIL>
    <CLFAX></CLFAX>
    <CLCONTACTO></CLCONTACTO>
    <CLEMAIL></CLEMAIL>
    <CLURL></CLURL>
  </Record>
  <Record>
    <CLNOMBRE>Empresa 2</CLNOMBRE>
    <CLCIF></CLCIF>
    <CLCATEGOR></CLCATEGOR>
    <CLNOTAS></CLNOTAS>
    <CLDIRECC></CLDIRECC>
    <CLLOCALI></CLLOCALI>
    <CLPAIS></CLPAIS>
    <CLTELEFONO></CLTELEFONO>
    <CLMOVIL></CLMOVIL>
    <CLFAX></CLFAX>
    <CLCONTACTO></CLCONTACTO>
    <CLEMAIL></CLEMAIL>
    <CLURL></CLURL>
  </Record>
</Data>
</ROOT>
 
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6772
Joined: Thu Oct 18, 2012 7:17 pm

Re: XML reader

Postby Antonio Linares » Wed Feb 27, 2013 9:41 am

Silvio,

It seems as this code requires to supply a parameter for the first name to look for:

Local oXmlNode := oMnuXml:FindFirst( "Header" )

In other words, this fails:

Local oXmlNode := oMnuXml:FindFirst()

I would say it seems a bug in the class TXml
regards, saludos

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

Re: XML reader

Postby Silvio.Falconi » Wed Feb 27, 2013 9:46 am

can see wich xml file you use ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6772
Joined: Thu Oct 18, 2012 7:17 pm

Re: XML reader

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

This version is simpler and should work fine with any 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: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: XML reader

Postby Silvio.Falconi » Fri Mar 01, 2013 9:25 am

good this run ok only we must insert our structure into to read the xml
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6772
Joined: Thu Oct 18, 2012 7:17 pm

Re: XML reader

Postby mtajkov » Sun Feb 02, 2014 8:48 pm

How to read xml file that has the left tag &lt; and right tag &gt; with:

Code: Select all  Expand view
  local hFile    := FOpen( "xOdgovor.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 )
 


If I change &lt; to < and &gt; to > everything works just fine.

Regards,
Milos
Best regards
Milos

[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
mtajkov
 
Posts: 126
Joined: Sun Mar 08, 2009 4:33 pm

Re: XML reader

Postby devtuxtla » Sun Feb 02, 2014 10:08 pm

Hi Antonio.

Thanks for the example.

Where I can find documented all the functions for creating XML?

I have not found many examples for creating XML

regards
Visite Chiapas, el paraiso de México.
devtuxtla
 
Posts: 392
Joined: Tue Jul 29, 2008 1:55 pm

Re: XML reader

Postby TimStone » Tue Feb 04, 2014 9:52 pm

Antonio,

The XML test code is all formatted with carriage returns.

I have now been informed that the XML standard does not need the carriage returns.

How would your reader be with all of the code in one continuous line.

Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2904
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: XML reader

Postby James Bott » Thu Feb 06, 2014 5:49 pm

Tim,

Have you tried it?

You could always preprocess it to add the carriage returns, although I agree it would be better if it handled the file without carriage returns.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: XML reader

Postby goosfancito » Fri Nov 17, 2023 9:30 am

Hola a todos,

Si tengo que ingresar a este nodo "DatiGeneraliDocumento"
<FatturaElettronicaBody>
<DatiGenerali>
<DatiGeneraliDocumento>

para poder obtener un dato, como hago? porque "DatiGeneraliDocumento" tengo varios

pensé que podía hacer esto pero no funciona:
Code: Select all  Expand view
  oNodo    := oxmldoc:findFirst("FatturaElettronicaBody", "DatiGenerali", "DatiGeneraliDocumento")

Gracias.
FWH 21.02
Harbour 3.2.0dev (r2104281802)
Copyright (c) 1999-2021, https://harbour.github.io/
User avatar
goosfancito
 
Posts: 1954
Joined: Fri Oct 07, 2005 7:08 pm

Re: XML reader

Postby nageswaragunupudi » Fri Nov 17, 2023 7:55 pm

FWH has an old forgotten/less known function
Code: Select all  Expand view
FW_XmlView( [filename.xml] )


If the function is called without parameters, it provides a file picker to choose an xml file and display.

Please try with xHarbour to view any xml file with any complexity.

Important Note: This function is not now working with Harbour. Has some bugs. Working with some files and fails with some other files.
For now please try with xHarbour only.

Simple xml.prg:
Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   FW_XmlView()

return nil


Sample:
Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10248
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: XML reader

Postby goosfancito » Fri Nov 17, 2023 8:38 pm

Hola.
Estoy necesitando extraer los datos de un xml, por eso necesito saber como hago para extraer los datos de una determinada rama. gracias.
FWH 21.02
Harbour 3.2.0dev (r2104281802)
Copyright (c) 1999-2021, https://harbour.github.io/
User avatar
goosfancito
 
Posts: 1954
Joined: Fri Oct 07, 2005 7:08 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 89 guests