FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour XML reader
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
XML reader
Posted: Wed Feb 27, 2013 07: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 (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: 7318
Joined: Thu Oct 18, 2012 07:17 PM
Re: XML reader
Posted: Wed Feb 27, 2013 08: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 March-April 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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: XML reader
Posted: Wed Feb 27, 2013 08:27 AM

Silvio,

They are available in both Harbour and xHarbour

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 7318
Joined: Thu Oct 18, 2012 07:17 PM
Re: XML reader
Posted: Wed Feb 27, 2013 09: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 (fw): Select all Collapse
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 (fw): Select all Collapse
<?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 March-April 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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: XML reader
Posted: Wed Feb 27, 2013 09: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
Posts: 7318
Joined: Thu Oct 18, 2012 07:17 PM
Re: XML reader
Posted: Wed Feb 27, 2013 09: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 March-April 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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: XML reader
Posted: Wed Feb 27, 2013 12:01 PM
This version is simpler and should work fine with any 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: 7318
Joined: Thu Oct 18, 2012 07:17 PM
Re: XML reader
Posted: Fri Mar 01, 2013 09: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 March-April 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
Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: XML reader
Posted: Sun Feb 02, 2014 08:48 PM
How to read xml file that has the left tag &lt; and right tag &gt; with:

Code (fw): Select all Collapse
   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) ]
Posts: 392
Joined: Tue Jul 29, 2008 01:55 PM
Re: XML reader
Posted: 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.
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: XML reader
Posted: Tue Feb 04, 2014 09: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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: XML reader
Posted: Thu Feb 06, 2014 05: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

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1956
Joined: Fri Oct 07, 2005 07:08 PM
Re: XML reader
Posted: Fri Nov 17, 2023 09: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 (fw): Select all Collapse
   oNodo    := oxmldoc:findFirst("FatturaElettronicaBody", "DatiGenerali", "DatiGeneraliDocumento")
Gracias.
FWH 21.02
Harbour 3.2.0dev (r2104281802)
Copyright (c) 1999-2021, https://harbour.github.io/
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: XML reader
Posted: Fri Nov 17, 2023 07:55 PM
FWH has an old forgotten/less known function
Code (fw): Select all Collapse
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 (fw): Select all Collapse
#include "fivewin.ch"

function Main()

   FW_XmlView()

return nil
Sample:
Regards



G. N. Rao.

Hyderabad, India
Posts: 1956
Joined: Fri Oct 07, 2005 07:08 PM
Re: XML reader
Posted: Fri Nov 17, 2023 08: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/

Continue the discussion