XML reader

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 42520
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 75 times
Contact:

XML reader

Post by Antonio Linares »

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

#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
Silvio.Falconi
Posts: 7136
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: XML reader

Post by Silvio.Falconi »

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
Antonio Linares
Site Admin
Posts: 42520
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 75 times
Contact:

Re: XML reader

Post by Antonio Linares »

Silvio,

They are available in both Harbour and xHarbour
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Silvio.Falconi
Posts: 7136
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: XML reader

Post by Silvio.Falconi »

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



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



<?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
Antonio Linares
Site Admin
Posts: 42520
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 75 times
Contact:

Re: XML reader

Post by Antonio Linares »

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
Silvio.Falconi
Posts: 7136
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: XML reader

Post by Silvio.Falconi »

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
Antonio Linares
Site Admin
Posts: 42520
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 75 times
Contact:

Re: XML reader

Post by Antonio Linares »

This version is simpler and should work fine with any XML :-)

xmlreader.prg

Code: Select all | Expand

#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

<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
Silvio.Falconi
Posts: 7136
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: XML reader

Post by Silvio.Falconi »

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
mtajkov
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: XML reader

Post by mtajkov »

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

Code: Select all | Expand

  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) ]
devtuxtla
Posts: 392
Joined: Tue Jul 29, 2008 1:55 pm

Re: XML reader

Post by devtuxtla »

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.
User avatar
TimStone
Posts: 2955
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Has thanked: 25 times
Been thanked: 2 times
Contact:

Re: XML reader

Post by TimStone »

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
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: XML reader

Post by James Bott »

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
goosfancito
Posts: 1955
Joined: Fri Oct 07, 2005 7:08 pm

Re: XML reader

Post by goosfancito »

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

   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
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: XML reader

Post by nageswaragunupudi »

FWH has an old forgotten/less known function

Code: Select all | Expand

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

#include "fivewin.ch"

function Main()

   FW_XmlView()

return nil
Sample:
Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
goosfancito
Posts: 1955
Joined: Fri Oct 07, 2005 7:08 pm

Re: XML reader

Post by goosfancito »

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/
Post Reply