delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (14)


Create a XML-file with data from some datasetGo to Mike Shkolnik's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
XML
Skill Level:
Scoring:
Last Update:
02/05/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet XML dataset export generate
Times Scored:
18
Visits:
27221
Uploader: Mike Shkolnik
Company: Scalabium Software
Reference: http://www.scalabium.com
 
Question/Problem/Abstract:
How to generate the XML-file from linked dataset?
Answer:



Sometimes in our development we must export a data from dataset into different formats like MS Excel, Word, HTML, Text etc. Now in the Internet we have a new popular format - XML-file. So for large part of applications we wants to include the possibility of export into XML, of course.
I want to demonstrate the sample of one procedure for exporting of dataset's data into XML:

procedure DatasetToXML(Dataset: TDataset; FileName: string);

The first Dataset parameter is source dataset with data (yout Table or Query component, or someelse third-party dataset).
The second FileName parameter is a name of target XML-file.


{ SMExport suite's free demo
  Data export from dataset into XML-file

  Copyright(C) 2000, written by Shkolnik Mike
  E-Mail:  mshkolnik@rs-ukraine.kiev.ua
           mshkolnik@yahoo.com
  WEB: http://www.scalabium.com
       http://www.geocities.com/mshkolnik
}
unit DS2XML;

interface

uses
  Classes, DB;

procedure DatasetToXML(Dataset: TDataset; FileName: string);

implementation

uses
  SysUtils;

var
  SourceBuffer: PChar;

procedure WriteString(Stream: TFileStream; s: string);
begin
  StrPCopy(SourceBuffer, s);
  Stream.Write(SourceBuffer[0], StrLen(SourceBuffer));
end;

procedure WriteFileBegin(Stream: TFileStream; Dataset: TDataset);

  function XMLFieldType(fld: TField): string;
  begin
    case fld.DataType of
      ftString: Result := '"string" WIDTH="' + IntToStr(fld.Size) + '"';
      ftSmallint: Result := '"i4"'; //??
      ftInteger: Result := '"i4"';
      ftWord: Result := '"i4"'; //??
      ftBoolean: Result := '"boolean"';
      ftAutoInc: Result := '"i4" SUBTYPE="Autoinc"';
      ftFloat: Result := '"r8"';
      ftCurrency: Result := '"r8" SUBTYPE="Money"';
      ftBCD: Result := '"r8"'; //??
      ftDate: Result := '"date"';
      ftTime: Result := '"time"'; //??
      ftDateTime: Result := '"datetime"';
    else
    end;
    if fld.Required then
      Result := Result + ' required="true"';
    if fld.Readonly then
      Result := Result + ' readonly="true"';
  end;

var
  i: Integer;
begin
  WriteString(Stream, '
  ' +>
                      '');
  WriteString(Stream, '');

  {write th metadata}
  with Dataset do
    for i := 0 to FieldCount-1 do
    begin
      WriteString(Stream, '');
    end;
  WriteString(Stream, '
');
  WriteString(Stream, '');
  WriteString(Stream, '
');
end;

procedure WriteFileEnd(Stream: TFileStream);
begin
  WriteString(Stream, '
');
end;

procedure WriteRowStart(Stream: TFileStream; IsAddedTitle: Boolean);
begin
  if not IsAddedTitle then
    WriteString(Stream, 'end;

procedure WriteRowEnd(Stream: TFileStream; IsAddedTitle: Boolean);
begin
  if not IsAddedTitle then
    WriteString(Stream, '/>');
end;

procedure WriteData(Stream: TFileStream; fld: TField; AString: ShortString);
begin
  if Assigned(fld) and (AString <> '') then
    WriteString(Stream, ' ' + fld.FieldName + '="' + AString + '"');
end;

function GetFieldStr(Field: TField): string;

  function GetDig(i, j: Word): string;
  begin
    Result := IntToStr(i);
    while (Length(Result) < j) do
      Result := '0' + Result;
  end;

var Hour, Min, Sec, MSec: Word;
begin
  case Field.DataType of
    ftBoolean: Result := UpperCase(Field.AsString);
    ftDate: Result := FormatDateTime('yyyymmdd', Field.AsDateTime);
    ftTime: Result := FormatDateTime('hhnnss', Field.AsDateTime);
    ftDateTime: begin
                  Result := FormatDateTime('yyyymmdd', Field.AsDateTime);
                  DecodeTime(Field.AsDateTime, Hour, Min, Sec, MSec);
                  if (Hour <> 0) or (Min <> 0) or (Sec <> 0) or (MSec <> 0) then
                    Result := Result + 'T' + GetDig(Hour, 2) + ':' + GetDig(Min, 2) + ':' + GetDig(Sec, 2) + GetDig(MSec, 3);
                end;
  else
    Result := Field.AsString;
  end;
end;



procedure DatasetToXML(Dataset: TDataset; FileName: string);
var
  Stream: TFileStream;
  bkmark: TBookmark;
  i: Integer;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  SourceBuffer := StrAlloc(1024);
  WriteFileBegin(Stream, Dataset);

  with DataSet do
  begin
    DisableControls;
    bkmark := GetBookmark;
    First;

    {write a title row}
    WriteRowStart(Stream, True);
    for i := 0 to FieldCount-1 do
      WriteData(Stream, nil, Fields[i].DisplayLabel);
    {write the end of row}
    WriteRowEnd(Stream, True);

    while (not EOF) do
    begin
      WriteRowStart(Stream, False);
      for i := 0 to FieldCount-1 do
        WriteData(Stream, Fields[i], GetFieldStr(Fields[i]));
      {write the end of row}
      WriteRowEnd(Stream, False);

      Next;
    end;

    GotoBookmark(bkmark);
    EnableControls;
  end;

  WriteFileEnd(Stream);
  Stream.Free;
  StrDispose(SourceBuffer);
end;

end.


PS: I created this simple procedure from own TSMExportToXML component which have a lot of useful extensions in export process. This component is part of shareware SMExport suite for Delphi/C++Builder so I cutted a part of code and created a simple procedure for exporting into XML.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Can I use TXMLBroker to save dataset as xml-file?
    boyface (Feb 27 2001 2:10AM)

Now, Delphi5 has genarated the TXMLBroker component.
I think using this component to perform dataset2xml is very easy.
Respond

RE: Can I use TXMLBroker to save dataset as xml-file?
boyface (Feb 27 2001 2:25AM)

Sorry, after the comment, i found that the same topic has been commented. And i confused the component's name.
Respond

drgfthdfgjhdjhdgh dghjdghj
    gfhsfhs (Dec 5 2000 12:49PM)

dghj dghj dghj dgh
j dghj
dgh
jdghjd ghjdghdj ghjgghjgf
Respond

RE: drgfthdfgjhdjhdgh dghjdghj
sensible (Jul 26 2005 2:04AM)

moron, wht the hell is that!!!!
Respond

XML - data in Delphi 5
    Jens Fudge (Jul 14 2000 10:17AM)

The article is most likely absolutely correct, in Delphi 5 however there is a simpler way of saving the resultset in a xml-file...

drop a TClientDataSet and a TdataSetProvider on the form.
set the TDatasetProvider.dataset to point to your TQuery.
Set the TClientDataSet.ProviderName to point to the TDataSetProvider.

In the code write:
ClientDataSet.active := true;
ClentDataSet.SaveToFile(`c:\results.xml`);

as long as the type name of the file is xml, it will be a xml-file.
Respond

RE: XML - data in Delphi 5
Mike Shkolnik (Jul 14 2000 10:25AM)

You forgot that you must deploy a few dlls for ClientDataset support!
In my sample - pure VCL without any additional libraries.
Respond

RE: XML - data in Delphi 5
Greg Burns (Jan 22 2001 7:01AM)

The TDataSet Provider and TClientDataset are very useful alternatives and more easily implemented because Inprise has built component wrappers around the MSXML_TLB interfaces. However, when you use this technique, you run the risk of liscensing issues with the MIDAS product, especially when you are accessing data across a network.
Respond

I
    Eddie Shipman (Jun 9 2000 9:19PM)

I`ve done a component similar, however, I built my XML using the
OpenXML DOM available at: http://www.philo.de/xml/

It may behoove you to take a look at it and start using it. Very easy
to learn and utilize. I originally did my component like you, by
building the XML file as a string, but changed when I found this.

I also am formatting my XML somewhat different than you and
building a DTD on-the-fly. I am in the process of working out a
way to build a schema on-the-fly as well, based upon the latest schema
standards at w3c.


Respond

RE: I
Mike Shkolnik (Jun 12 2000 9:09AM)

Eddie,

I saw this component but I sure that to include the 400Kb of component sources in own application for small task (to create a xml-file) is not good solution. But the TXML* component from Dieter is really good for large applications which must open the xml-files.

About formatting - the TSMExportToXML component from SMExport suite supports a few formatting types (including the TClientDataset support) but in articles I posted a small example only.
Respond

Error in Code
    anonymus (May 24 2000 11:10PM)

Hi Mike. Could you please repost the code in the article to correct this part of the Unit? It seems to be truncated.

Thanks.


procedure WriteRowStart(Stream: TFileStream; IsAddedTitle: Boolean);
begin
  if not IsAddedTitle then
    WriteString(Stream, 'end;

Respond

RE: Error in Code
Mike Shkolnik (May 25 2000 10:01AM)

I wrote a message to Bernhard because I sure it's a bug in delphi3000's system: in the Edit mode the body of article is not corrupted but in usual view mode the one procedure is truncated:(

Now anybody, who wants to receive the full text of this unit, can write to me by email and I'll send it to you.
I sure that Bernhard'll fix this bug in few days.
Respond

RE: RE: Error in Code
anonymus (May 25 2000 1:44PM)

Thanks to Stefan Walther and Bernhard Angerer for your quick response and fix.
Respond

RE: RE: RE: Error in Code
Tom Ridgeway (Feb 13 2001 5:51PM)

If it was fixed last spring, it is back to unfixed now --
procedure WriteRowStart(Stream: TFileStream; IsAddedTitle: Boolean);
begin
  if not IsAddedTitle then
    WriteString(Stream, 'end;


Respond

RE: Error in Code
Mike Shkolnik (Feb 14 2001 5:25AM)

I'll contact to webmaster but you can always to download a latest version of correct code at my site:
http://www.scalabium.com/faq/dct0079.htm

With best regards, Mike Shkolnik
E-Mail: mshkolnik@scalabium.com
        mshkolnik@yahoo.com
WEB: http://www.scalabium.com

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
S. Carter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)