FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour dbf to Xml
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: dbf to Xml
Posted: Tue Jul 21, 2009 07:23 AM

Hi all,

Is there any Sample PRG available demonstrating how to convert a DBf to XML programatically using Native FWH xHarbour without using ADO. Mr.Enrico has already provided a sample above in this thread showing the conversion using ADO

Is there any class available to convert a DBF to XML.

Regards
Anser

Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM
Re: dbf to Xml
Posted: Tue Jul 21, 2009 07:41 AM
Anser,

this function may help you

Code (fw): Select all Collapse
/*
/  Dbf2Xml. Utilidad de conversion de ficheros DBF a XML
/  mediante transformacion XSL con salida HTML
/  (C) 2003. Joaquim Ferrer Godoy
/  Inicio: 16-07-2003                                                       */
#define CRLF chr(13)+chr(10)

function main( cDbf, cXmlOut )

  local aStruc, aNomFields
  local nHandle
  cXmlOut := If( cXmlOut = NIL, cDbf, cXmlOut )

   if cDbf = NIL
      ? "Es necesario indicar el nombre del archivo DBF"
      return( .f. )
   endif

   if !file( cDbf + ".dbf" )
      ? "No existe el archivo DBF especificado"
      return( .f. )
   endif

   USE ( cDbf ) NEW ALIAS "_temp_"

   //Obtener la lista de campos
   aStruc     := dbstruct()
   aNomFields := {}
   aeval( aStruc, {|a| aadd( aNomFields, a[1] ) } )

   // Proceso de escritura del archivo de salida XML
   ? "Generando XML : " + cXmlOut
   nHandle := fcreate( cXmlOut + ".xml" )
   fwrite( nHandle, '<?xml version="1.0" encoding="ISO8859-1" ?>' + CRLF )
   fwrite( nHandle, "<DATABASE>" + CRLF )
   _temp_->( dbgotop() )
   do while !_temp_->( eof() )
      fwrite( nHandle, "<RECORD>" + CRLF )
      aeval( aNomFields, {|cField, nPos| ;
                         fwrite( nHandle, "<" + cField + ">" +;
                         Val2Char( _temp_->( fieldget( nPos ) ) ) + ;
                         "</" + cField + ">" + CRLF ) } )
      fwrite( nHandle, "</RECORD>" + CRLF )
   _temp_->( dbskip() )
   enddo
   fwrite( nHandle, "</DATABASE>" + CRLF )
   fclose( nHandle )
   _temp_->( dbclosearea() )

   // Proceso de escritura del archivo de salida XSL
   // XSL realizara la transformacion del XML
   ? "Generando XSL : " + cXmlOut
   nHandle := fcreate( cXmlOut + ".xsl" )
   fwrite( nHandle, "<?xml version='1.0'?>" + CRLF )
   fwrite( nHandle, '<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">' + CRLF )
   fwrite( nHandle, '<xsl:template match="/">' + CRLF )
   fwrite( nHandle, '<html>' + CRLF )
   fwrite( nHandle, '<body>' + CRLF )
   fwrite( nHandle, '<table border="1" cellpadding="2" bgcolor="#f1f1f1" width="100%">' + CRLF )
   fwrite( nHandle, '<tr>' + CRLF )
   aeval( aNomFields, {|cField| ;
                         fwrite( nHandle, "<th>" + cField + "</th>" + CRLF ) } )
   fwrite( nHandle, '</tr>' + CRLF )
   fwrite( nHandle, '<xsl:for-each select="DATABASE/RECORD">' + CRLF )
   fwrite( nHandle, '<tr>' + CRLF )
   aeval( aNomFields, {|cField| ;
                         fwrite( nHandle, '<td><xsl:value-of select="' + ;
                         cField + '"/></td>' + CRLF ) } )
   fwrite( nHandle, '</tr>' + CRLF )
   fwrite( nHandle, '</xsl:for-each>' + CRLF )
   fwrite( nHandle, '</table>' + CRLF )
   fwrite( nHandle, '</body>' + CRLF )
   fwrite( nHandle, '</html>' + CRLF )
   fwrite( nHandle, '</xsl:template>' + CRLF )
   fwrite( nHandle, '</xsl:stylesheet>' + CRLF )
   fclose( nHandle )

   // Proceso de escritura del archivo de salida HTML
   ? "Generando HTML: " + cXmlOut
   nHandle := fcreate( cXmlOut + ".htm" )
   fwrite( nHandle, '<html>' + CRLF )
   fwrite( nHandle, '<head>' + CRLF )
   fwrite( nHandle, '<title>Dbf2XML : ' + cXmlOut + '</title>' + CRLF )
   fwrite( nHandle, '<style>' + CRLF )
   fwrite( nHandle, 'TH{font-family:verdana;font-size:12px}' + CRLF )
   fwrite( nHandle, 'TD{font-family:verdana;font-size:10px}' + CRLF )
   fwrite( nHandle, '</style>' + CRLF )
   fwrite( nHandle, '</head>' + CRLF )
   fwrite( nHandle, '<body>' + CRLF )
   fwrite( nHandle, '<script language="javascript">' + CRLF )
   fwrite( nHandle, 'var xml = new ActiveXObject("Microsoft.XMLDOM")' + CRLF )
   fwrite( nHandle, 'xml.async = false' + CRLF )
   fwrite( nHandle, 'xml.load("' + cXmlOut + '.xml")' + CRLF )
   fwrite( nHandle, 'var xsl = new ActiveXObject("Microsoft.XMLDOM")' + CRLF )
   fwrite( nHandle, 'xsl.async = false' + CRLF )
   fwrite( nHandle, 'xsl.load("' + cXmlOut + '.xsl")' + CRLF )
   fwrite( nHandle, 'document.write(xml.transformNode(xsl))' + CRLF )
   fwrite( nHandle, '</script>' + CRLF )
   fwrite( nHandle, '</body>' + CRLF )
   fwrite( nHandle, '</html>' + CRLF )
   fclose( nHandle )

return( .t. )

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

static func Val2Char( uVar )

  local cChar
  local cType := valtype( uVar )

  do case
     case cType == "N"
     cChar := rtrim( str( uVar ) )
     case cType == "D"
     cChar := dtos( uVar )
     case cType == "L"
     cChar := If( uVar, ".T.", ".F." )
  otherwise
     cChar := uVar   // Caracter
  endcase

return( rtrim( cChar ) )
kind regards

Stefan
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: dbf to Xml
Posted: Tue Jul 21, 2009 07:57 AM

Thank you Mr.Stefan

Regards
Anser

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: dbf to Xml
Posted: Mon Aug 03, 2009 03:52 PM
>Is there any class available to convert a DBF to XML.

There is another DBF to XML conversion program on my website here:

http://www.goIntellitech.com/program.htm

Regards,
James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: dbf to Xml
Posted: Mon Aug 03, 2009 04:01 PM

Dear Mr.James,

Thank you for the information. Very useful code

Regards
Anser

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: dbf to Xml
Posted: Mon Aug 03, 2009 04:28 PM

Enrico,

When I try to run your XML to DBF code (using ADO), I get the following error:

Error description: Error ADODB.Recordset/6 DISP_E_UNKNOWNNAME: OPEN
Args:
[ 1] = O Object

Stack Calls

Called from: source\rtl\win32ole.prg => TOLEAUTO:OPEN(0)
Called from: XML2DBF3.prg => MAIN(24)

It is happening at this line of your code:

oStream:Open()

The oStream object is getting defined as an object. Any ideas?

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: dbf to Xml
Posted: Mon Aug 03, 2009 08:17 PM

Can you send to my private email the XML file you are testing?

Be patient as I will probably can't answer in the next days.

EMG

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: dbf to Xml
Posted: Tue Aug 04, 2009 01:23 AM
Enrico,

It doesn't have anything to do with the XML file as the code hasn't tried to open the file yet. Here is the only code that has been executed:

Code (fw): Select all Collapse
FUNCTION MAIN()

    LOCAL oStream, oRs

    oStream = CREATEOBJECT( "ADODB.Stream" )

    msgInfo( valtype( oStream ) )  // returns "O"

    oStream:Open()


It is erroring out on the Open() method. The error appears to be saying that there is no "Open" method.

No worries about a timely reply. I don't need this right away.

Regards,
James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: dbf to Xml
Posted: Tue Aug 04, 2009 09:16 AM

Mr James

Your code is working perfect for me. ( on XP )
I am not getting any errors.
MsgInfo( oStream:State ) --> returns 1 ( 1 for opened )

Will you please check again ?

Regards



G. N. Rao.

Hyderabad, India
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: dbf to Xml
Posted: Tue Aug 04, 2009 02:26 PM

Rao,

>MsgInfo( oStream:State ) --> returns 1

oStream:State is returning 0 for me (under XP Pro, SP3).

This is my concern and that of others. Even though oStream is showing as an object, apparently the needed components are not installed on my computer. If you are creating an application for a single site, then perhaps using ADO is a good choice. But, if you are writing applications for use on many sites, then ADO is going to cause you a great deal of tech support. This is the reason many of us do not use ADO.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: dbf to Xml
Posted: Tue Aug 04, 2009 03:56 PM

Mr James

I agree with you.
But to the best of my knowledge and experience, ADO is working on all PCs with XP, with the default installation. This is my experience with an organization with about 500 XPs and also that of my friends here.

In fact I am surprised why it is not working on your XP or some other XPs. May be my exposure is limited.

Regards



G. N. Rao.

Hyderabad, India
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: dbf to Xml
Posted: Tue Aug 04, 2009 04:45 PM

Rao,

After reading your comments about it working on all your XP PCs, I took another look.

My mistake! I had not looked at the line number of the error, and I assumed it was erroring out on:

oStream:Open()

But, in fact, it was erroring out on:

oRS:Open( oStream )

But, I still don't know why I am getting this error:

Error ADODB.Recordset/6 DISP_E_UNKNOWNNAME: OPEN

I have checked and oRs is an object type, but the error message is indicating that it doesn't have an Open() method.

Perhaps it IS a problem with the sample XML file I was using as Enrico suggested. I don't remember where I got this file, and I don't know what format ADODB is expecting.

Can you send me a sample XML file that is working for you? Send it to:

jbott at compuserve dot com

Thanks,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: dbf to Xml
Posted: Sat Aug 08, 2009 02:16 PM

Enrico,

If you sent it to me, I didn't get it. Please try again.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM