For Programmers: Free Programming Magazines  


Home > Archive > Clipper > March 2005 > Re: conversion of file from one format to other









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: conversion of file from one format to other
pete@nospam.demon.co.uk

2005-03-22, 3:55 am

In article <3a865vF64k847U1@individual.net>
sschavan_REMOVE_ME_@eth.net "rajeev chavan" writes:

> Hi everybody


Hi Rajeev,

> I have 2 problems
> 1) I need to convert a file in ASCII(comma delimited) format to DBF
> format.The format of ASCII file is as follows.


[snip details of CSV and DBF layouts]

> My problem is each of the line in text file is appended as a record.Actually
> each record starts at date.Could anyone suggest some way of appending above
> text file to DBF file.


There are a couple of ways I'd approach this, both using a
temporary or intermediate DBF

(a) create a temp dbf structure whose fields match exactly the
layout of the csv and APPEND DELIMITED to that (but watch out for
that "date" field; you'll have to make it character type and
convert it later). Then use this temp dbf to copy data into your
final target -- you can make life easier for yourself by choosing
matching field names :-)

(b) create a temp dbf structure with a single, long field -- for
example WHOLELINE, a character type with length (e.g.) 1000 and
then APPEND SDF from your csv file. This will contain all the
data -- commas included -- which you can then parse at your
leisure using AT() and SUBSTR() to retrieve the individual
fields. Then copy across/append to your final DBF.

> 2) I need to convert DBF file to XML format for importing it in some other
> program.Is it possible to do so ? How ?


You could either write the code to do that, or look for a tool on
the web. There is a site/compamy that seems to specialise in
file conversion tools and whose name escapes me at the moment
(old age :-() but a search for "dbf xml converter" should find
you something.

Pete
--
"We have not inherited the earth from our ancestors,
we have borrowed it from our descendants."
Ross McKenzie

2005-03-22, 3:55 am

On Tue, 22 Mar 2005 06:21:03 +0000 (UTC), pete@nospam.demon.co.uk
wrote:

>In article <3a865vF64k847U1@individual.net>
> sschavan_REMOVE_ME_@eth.net "rajeev chavan" writes:
>
>
>Hi Rajeev,


>
>
>You could either write the code to do that, or look for a tool on
>the web. There is a site/compamy that seems to specialise in
>file conversion tools and whose name escapes me at the moment
>(old age :-() but a search for "dbf xml converter" should find
>you something.
>
>Pete
>--


Hi Pete and Rajeev,

Here is one approach for part 2.


//---------------------------------------------------------
// Dbf2XML Version 1.0
//
// This program converts a dbf file to xml file
// Author : Yamil Bracho, Caracas, Venezuela
// brachoy@pdvsa.com
//
// Date : Nov 2000
// Note : Compile with /n
// Revised: 7/29/02 Added special character processing.
// Fixed a few bugs. James Bott. jbott@compuserve.com
//---------------------------------------------------------

#include "FileIo.ch"
#define CRLF Chr( 13 ) + Chr( 10 )


FUNCTION Main( cDbf )
LOCAL cFile
LOCAL nPos

IF cDbf == NIL
? "dbf2xml Ver.1.0"
? "Usage:dbf2xml <dbf_file>"
QUIT
ELSE
cFile:= cDbf
nPos := At( ".dbf", cFile )
IF nPos == 0
cFile := cFile + ".dbf"
ENDIF

IF File( cFile ) == .F.
? cFile + " does not exist."
QUIT
ELSE
GenXML( cFile )
? "Converted to XML"
ENDIF
ENDIF

RETURN NIL



//---------------------------------------------------------
// Generates the file
//---------------------------------------------------------
STATIC FUNCTION GenXML( cDbf )
LOCAL aFields
LOCAL cBuffer
LOCAL cFile
LOCAL cValue
LOCAL cTable
LOCAL nHandle
LOCAL nFields
LOCAL nField
LOCAL nPos

cDBF := lower(cDBF)
cFile := StrTran( cDbf, ".dbf", ".xml" )
cTable := Left( cDbf, At( ".", cDbf ) - 1 )

USE (cDbf)
nHandle := fCreate( cFile, FC_NORMAL )

//------------------
// Writes XML header
//------------------
fWrite( nHandle, [<?xml version="1.0"?>] + CRLF )
fWrite( nHandle, Space( 0 ) + "<" + cDbf + ">" + CRLF )

nFields := fCount()
aFields := dbStruct()

DO WHILE .NOT. Eof()
cBuffer := Space( 2 ) + "<" + cTable + ">" + CRLF
fWrite( nHandle, cBuffer )

FOR nField := 1 TO nFields

//-------------------
// Beginning Record Tag
//-------------------

cBuffer:= Space( 4 ) + "<" + FieldName( nField ) + ">"

DO CASE
CASE aFields[nField, 2] == "D"
cValue := Dtos( FieldGet( nField ))

CASE aFields[nField, 2] == "N"
cValue := Str( FieldGet( nField ))

CASE aFields[nField, 2] == "L"
cValue := If( FieldGet( nField ), "True", "False" )

OTHERWISE
cValue := FieldGet( nField )
ENDCASE

//--- Convert special characters
cValue:= strTran(cValue,"&","&")
cValue:= strTran(cValue,"<","&lt;")
cValue:= strTran(cValue,">","&gt;")
cValue:= strTran(cValue,"'","'")
cValue:= strTran(cValue,["],["])

cBuffer := cBuffer + ;
Alltrim( cValue ) + ;
"</" + ;
FieldName( nField ) + ;
">" + ;
CRLF

fWrite( nHandle, cBuffer )
NEXT nField

//------------------
// Ending Record Tag
//------------------
fWrite( nHandle, Space( 2 ) + "</" + cTable + ">" + CRLF )
SKIP
ENDDO

dbCloseAll()
fWrite( nHandle, Space(0) + "</" + cDbf + ">" + CRLF )
fClose( nHandle )
RETURN NIL

// eof



Regards,

Ross McKenzie
ValuSoft
Melbourne Australia

valusoft AT optushome DOT com DOT au

I love deadlines...I particularly enjoy the whooshing noise they make as they fly by.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com