Page 1 of 1

XML reader

PostPosted: Wed Feb 27, 2013 7:16 am
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 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
 

Re: XML reader

PostPosted: Wed Feb 27, 2013 8:08 am
by Silvio.Falconi
Antonio,
just to try it , these classes are in fwh, xharbour or harbour?

Re: XML reader

PostPosted: Wed Feb 27, 2013 8:27 am
by Antonio Linares
Silvio,

They are available in both Harbour and xHarbour

Re: XML reader

PostPosted: Wed Feb 27, 2013 9:09 am
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 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>
 

Re: XML reader

PostPosted: Wed Feb 27, 2013 9:41 am
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

Re: XML reader

PostPosted: Wed Feb 27, 2013 9:46 am
by Silvio.Falconi
can see wich xml file you use ?

Re: XML reader

PostPosted: Wed Feb 27, 2013 12:01 pm
by Antonio Linares
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>

Re: XML reader

PostPosted: Fri Mar 01, 2013 9:25 am
by Silvio.Falconi
good this run ok only we must insert our structure into to read the xml

Re: XML reader

PostPosted: Sun Feb 02, 2014 8:48 pm
by mtajkov
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

Re: XML reader

PostPosted: Sun Feb 02, 2014 10:08 pm
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

Re: XML reader

PostPosted: Tue Feb 04, 2014 9:52 pm
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

Re: XML reader

PostPosted: Thu Feb 06, 2014 5:49 pm
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

Re: XML reader

PostPosted: Fri Nov 17, 2023 9:30 am
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 view
  oNodo    := oxmldoc:findFirst("FatturaElettronicaBody", "DatiGenerali", "DatiGeneraliDocumento")

Gracias.

Re: XML reader

PostPosted: Fri Nov 17, 2023 7:55 pm
by nageswaragunupudi
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

Re: XML reader

PostPosted: Fri Nov 17, 2023 8:38 pm
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.