DBToXML (Bileşen)

Yazdığınız makaleleri ve üyelerimizin işine yarayacağını düşündüğünüz kodlarınızı gönderebilirsiniz. Bu foruma soru sormayın!
Cevapla
Kullanıcı avatarı
vkamadan
Kıdemli Üye
Mesajlar: 1935
Kayıt: 17 Mar 2004 03:52
Konum: Adapazarı
İletişim:

DBToXML (Bileşen)

Mesaj gönderen vkamadan »

Merhaba,
Basit XML yapıları oluşturmak isterseniz bağladığınız Dataset e göre XML dosyasınızı oluşturacak bir bileşen hazırladım.

Kod: Tümünü seç

unit DbtoXML;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,db;

type
  TDbtoXML = class(TComponent)
  private
    FDataset:TDataset;
    FDosyaAdi:String;
    FKokDugumAdi:String;
    FKayitDugumuAdi:String;
    { Private declarations }
  protected
    { Protected declarations }
  public
    procedure XMLOlustur;
    { Public declarations }
  published
   property Dataset : TDataset read FDataset write FDataset;
   property DosyaAdi: String read FDosyaAdi write FDosyaAdi;
   property KokDugumAdi:String read FKokDugumAdi write FKokDugumAdi;
   property KayitDugumuAdi:String read FKayitDugumuAdi write FKayitDugumuAdi;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('vComponents', [TDbtoXML]);
end;

{ TDbtoXML }

procedure TDbtoXML.XMLOlustur;
var
i:integer;
XMLDosya:TStringList;
begin
    if Assigned(FDataset) then
     begin
          try
            XMLDosya:=TStringList.Create;

            FDataset.open;
            FDataset.First;
            XMLDosya.Add('<?xml version="1.0" encoding="iso-8859-9"?>');
            if Trim(FKokDugumAdi)<>'' then XMLDosya.Add('<'+FKokDugumAdi+'>') else XMLDosya.Add('<'+FDataset.Name+'>');

            while not FDataset.Eof do
             begin
               if Trim(FKayitDugumuAdi)<>'' then XMLDosya.Add('<'+FKayitDugumuAdi+'>') else XMLDosya.Add('<Kayit>');
               for i:=0 to FDataset.FieldCount-1 do  XMLDosya.Add('<'+FDataset.Fields[i].FieldName+'>'+FDataset.Fields[i].AsString+'</'+FDataset.Fields[i].FieldName+'>');
               if Trim(FKayitDugumuAdi)<>'' then XMLDosya.Add('</'+FKayitDugumuAdi+'>') else XMLDosya.Add('</Kayit>');
               FDataset.Next;
             end;
            if Trim(FKokDugumAdi)<>'' then XMLDosya.Add('</'+FKokDugumAdi+'>') else XMLDosya.Add('</'+FDataset.Name+'>');
            if XMLDosya.Count>0 then XMLDosya.SaveToFile(FDosyaAdi);
          finally
            XMLDosya.Free;
          end;

     end;
end;

end.
Kullanımı oldukça basit,
Hali hazırda TDataset ten türemiş nesnenizi(TQuery,TTable,TAdoQuery vs...), Dataset özelliğine bağlayın,
DosyaAdı na yoluyla birlikte oluşacak XML in adını belirtin,
Eğer KokDugum adı boş geçilirse , bağladığınız Dataset adı alınır, KayitDugumu adi bos geçilirse <Kayit> olarak isim verilir zaten kodu incelerseniz fark edeceksiniz.
Basit bir örnek, Query1 ve DbtoXML1 adında nesnelerden yola çıkıyorum

Kod: Tümünü seç

with Query1 Do
begin
 close;
 sql.clear;
 sql.add('select * from hesaplar');
 active:=True;
end;

with DbtoXML1 do
begin
 DataSet := Query1;
 DosyaAdi := 'c:\sonuc.xml'; 
 KokDugumAdi :='HesapKartlari' ;
 KayiDugumuAdi := 'Hesap';
 XMLOlustur;
end;

Bir çok açık olabilir geliştirebilirsiniz, değerli bir arkadaşıma acil lazım olmuştu onun için apar topar yaptım.
İyi çalışmalar.
Volkan KAMADAN
www.polisoft.com.tr
Cevapla