vkamadan yazdı: ↑30 Mar 2005 01:01
Merhaba arkadaşlar,
çok uzun süredir XML dosyalarıyla başım belada idi.
ve sorunuma çözüm için yaptığım araştırmalar sonucunda çeşitli 3.parti parse bileşenleri ile uğraşmaktansa özellikle tekrarlayan veri kümeleri içeren XML dosyalarına erişimi oldukça basite indirgeyen "XML Data Binding" sihirbazını kullanarak oluşturacağımız bir Arayüz sayesinde XML dosyamız üzerinde aklımıza gelen her türlü işlemi yapabileceğiz.
Şimdi adım adım örnek XML Dosyamıza XML Data Binding kullanarak nasıl erişileceğine bir göz atalım ;
Örnek olarak kullnacağımız ogrenci.xml dosyasının içeriği aşağıdaki gibidir.
Kod: Tümünü seç
<?xml version="1.0" encoding="windows-1254"?>
<BILGI>
<OgrenciNo>1229</OgrenciNo>
<OgrenciSinifi>8A</OgrenciSinifi>
<OgrenciAdi>Keramettin Sevcan</OgrenciAdi>
<TCKNo>53564654454</TCKNo>
<BolumKodu>ETR05</BolumKodu>
<BolumAdi>Elektronik</BolumAdi>
</BILGI>
öncelikle XML Data Binding sihirbazını kullanarak arayüz unit mizi oluşturalım.
File / New / Other menüsüne girerek , NEW tabındaki
XML Data Binding seçeneğini seçelim, karşımıza gelen ekranda
Shema or XML Data File a XML dosyamızın konumu belirterek NEXT diyelim bir sonraki ekranda XML dosyasının içeriği bir ağaç diyagram şeklinde arayüzümüzün içinde oluşturulacak metod ve özellikleri düzenleyebilecek şekilde karşımız gelir. eğer varsayılan metod ve özellik isimleri hoşumuza gitmezse (XML mizin elementleri türkçe karakter içermekte özgürdür yani alan isimleri türkçe olabilir bu durumda Delphinin oluşturacağı metod isimlerinin hoşunuza gideceğini sanmıyorum) gerekli düzenlemeleri yaparak NEXT tuşuna basarak arayüzmüzün içeriğini görebileceğimiz bitiş ekranına gelelim ve FINISH tuşuna basarak arayüzümüzün oluşturulmasını sağlayalım.
oluşan arayüz unitinin içeriği aşağıdaki gibi oalcaktır;
Kod: Tümünü seç
unit ogrenci;
interface
uses xmldom, XMLDoc, XMLIntf;
type
{ Forward Decls }
IXMLBILGIType = interface;
{ IXMLBILGIType }
IXMLBILGIType = interface(IXMLNode)
['{C1587B9B-0D0E-49D2-BCFA-CAFF3AB5280C}']
{ Property Accessors }
function Get_OgrenciNo: Integer;
function Get_OgrenciSinifi: WideString;
function Get_OgrenciAdi: WideString;
function Get_TCKNo: Integer;
function Get_BolumKodu: WideString;
function Get_BolumAdi: WideString;
procedure Set_OgrenciNo(Value: Integer);
procedure Set_OgrenciSinifi(Value: WideString);
procedure Set_OgrenciAdi(Value: WideString);
procedure Set_TCKNo(Value: Integer);
procedure Set_BolumKodu(Value: WideString);
procedure Set_BolumAdi(Value: WideString);
{ Methods & Properties }
property OgrenciNo: Integer read Get_OgrenciNo write Set_OgrenciNo;
property OgrenciSinifi: WideString read Get_OgrenciSinifi write Set_OgrenciSinifi;
property OgrenciAdi: WideString read Get_OgrenciAdi write Set_OgrenciAdi;
property TCKNo: Integer read Get_TCKNo write Set_TCKNo;
property BolumKodu: WideString read Get_BolumKodu write Set_BolumKodu;
property BolumAdi: WideString read Get_BolumAdi write Set_BolumAdi;
end;
{ Forward Decls }
TXMLBILGIType = class;
{ TXMLBILGIType }
TXMLBILGIType = class(TXMLNode, IXMLBILGIType)
protected
{ IXMLBILGIType }
function Get_OgrenciNo: Integer;
function Get_OgrenciSinifi: WideString;
function Get_OgrenciAdi: WideString;
function Get_TCKNo: Integer;
function Get_BolumKodu: WideString;
function Get_BolumAdi: WideString;
procedure Set_OgrenciNo(Value: Integer);
procedure Set_OgrenciSinifi(Value: WideString);
procedure Set_OgrenciAdi(Value: WideString);
procedure Set_TCKNo(Value: Integer);
procedure Set_BolumKodu(Value: WideString);
procedure Set_BolumAdi(Value: WideString);
end;
{ Global Functions }
function GetBILGI(Doc: IXMLDocument): IXMLBILGIType;
function LoadBILGI(const FileName: WideString): IXMLBILGIType;
function NewBILGI: IXMLBILGIType;
const
TargetNamespace = '';
implementation
{ Global Functions }
function GetBILGI(Doc: IXMLDocument): IXMLBILGIType;
begin
Result := Doc.GetDocBinding('BILGI', TXMLBILGIType, TargetNamespace) as IXMLBILGIType;
end;
function LoadBILGI(const FileName: WideString): IXMLBILGIType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('BILGI', TXMLBILGIType, TargetNamespace) as IXMLBILGIType;
end;
function NewBILGI: IXMLBILGIType;
begin
Result := NewXMLDocument.GetDocBinding('BILGI', TXMLBILGIType, TargetNamespace) as IXMLBILGIType;
end;
{ TXMLBILGIType }
function TXMLBILGIType.Get_OgrenciNo: Integer;
begin
Result := ChildNodes['OgrenciNo'].NodeValue;
end;
procedure TXMLBILGIType.Set_OgrenciNo(Value: Integer);
begin
ChildNodes['OgrenciNo'].NodeValue := Value;
end;
function TXMLBILGIType.Get_OgrenciSinifi: WideString;
begin
Result := ChildNodes['OgrenciSinifi'].Text;
end;
procedure TXMLBILGIType.Set_OgrenciSinifi(Value: WideString);
begin
ChildNodes['OgrenciSinifi'].NodeValue := Value;
end;
function TXMLBILGIType.Get_OgrenciAdi: WideString;
begin
Result := ChildNodes['OgrenciAdi'].Text;
end;
procedure TXMLBILGIType.Set_OgrenciAdi(Value: WideString);
begin
ChildNodes['OgrenciAdi'].NodeValue := Value;
end;
function TXMLBILGIType.Get_TCKNo: Integer;
begin
Result := ChildNodes['TCKNo'].NodeValue;
end;
procedure TXMLBILGIType.Set_TCKNo(Value: Integer);
begin
ChildNodes['TCKNo'].NodeValue := Value;
end;
function TXMLBILGIType.Get_BolumKodu: WideString;
begin
Result := ChildNodes['BolumKodu'].Text;
end;
procedure TXMLBILGIType.Set_BolumKodu(Value: WideString);
begin
ChildNodes['BolumKodu'].NodeValue := Value;
end;
function TXMLBILGIType.Get_BolumAdi: WideString;
begin
Result := ChildNodes['BolumAdi'].Text;
end;
procedure TXMLBILGIType.Set_BolumAdi(Value: WideString);
begin
ChildNodes['BolumAdi'].NodeValue := Value;
end;
end.
evet unitmizi kaydedelim. (örn:ogrenci.pas)
artık ogrenci.xml dosyasını referans alarak bu yapıdaki XML dosyaları üzerinde işlem yapabilme yeteneğine sahip olan
IXMLBilgitType arayüzünü içeren unitimizi oluşturduğumuza göre örnek uygulamamızı geliştirebiliriz.
bu örnekte ogrenci.xml dosyasının içindeki alanları listbox a ekleyeceğiz.
Yeni bir proje oluşturup USES kısmına "ogrenci" unitini ekleyelim.
bir listbox ve bir buton ekleyelim, Butonun OnClick olay metoduna yazalım ;
Kod: Tümünü seç
procedure TForm1.Button1Click(Sender: TObject);
var
XMLBilgi:IXMLBILGIType;
begin
XMLBilgi:= LoadBILGI('ogrenci.xml');
ListBox1.Items.add('No :'+IntToStr(XMLBilgi.OgrenciNo));
ListBox1.Items.add('Sınıf :'+XMLBilgi.OgrenciSinifi);
ListBox1.Items.add('Adı :'+XMLBilgi.OgrenciAdi);
ListBox1.Items.add('TCK No:'+XMLBilgi.TCKNo);
ListBox1.Items.add('BKodu :'+XMLBilgi.BolumKodu);
ListBox1.Items.add('BAdı :'+XMLBilgi.BolumAdi);
end;
burada belirtmek istediğim bir nokta, oluşturduğumuz arayüz uniti varolan bir XMLDocument nesnesini alan yada belirteceğimiz bir konumdaki XML dosyasını yükleyen yada yeni bir XML dosyası oluşturmaya olanak sağlayan 3 adet global fonksiyon sunar bir ogrenci.xml dosyanısı yüklemek için
function LoadBILGI(const FileName: WideString): IXMLBILGIType; fonksiyonu kullandık diğer global fonksiyonlar;
hazırdaki bir TXMLDocument nesnesini kullanarak işlem yapan;
function GetBILGI(Doc: IXMLDocument): IXMLBILGIType;
yapımıza uygun yeni bir XML oluşturan;
function NewBILGI: IXMLBILGIType;
fonksiyonlarınıda isteğe uygun kullanabiliriz.
ogrenci.xml , ogrenci.pas ve örnek projeyi içeren zip dosyasını siteye eklemesi için adminimize gönderiyorum.
iyi çalışmalar.