dbf to Xml

Re: dbf to Xml

Postby anserkk » Tue Jul 21, 2009 7: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
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: dbf to Xml

Postby StefanHaupt » Tue Jul 21, 2009 7:41 am

Anser,

this function may help you

Code: Select all  Expand view
/*
/  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
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: dbf to Xml

Postby anserkk » Tue Jul 21, 2009 7:57 am

Thank you Mr.Stefan

Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: dbf to Xml

Postby James Bott » Mon Aug 03, 2009 3: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: dbf to Xml

Postby anserkk » Mon Aug 03, 2009 4:01 pm

Dear Mr.James,

Thank you for the information. Very useful code

Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: dbf to Xml

Postby James Bott » Mon Aug 03, 2009 4: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: dbf to Xml

Postby Enrico Maria Giordano » Mon Aug 03, 2009 8: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
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: dbf to Xml

Postby James Bott » Tue Aug 04, 2009 1: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: Select all  Expand view
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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: dbf to Xml

Postby nageswaragunupudi » Tue Aug 04, 2009 9: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
User avatar
nageswaragunupudi
 
Posts: 10625
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: dbf to Xml

Postby James Bott » Tue Aug 04, 2009 2: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: dbf to Xml

Postby nageswaragunupudi » Tue Aug 04, 2009 3: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
User avatar
nageswaragunupudi
 
Posts: 10625
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: dbf to Xml

Postby James Bott » Tue Aug 04, 2009 4: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Re: dbf to Xml

Postby James Bott » Sat Aug 08, 2009 2:16 pm

Enrico,

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

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


PreviousNext

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 65 guests