
Borland
Delphi 6 Examples
This web
page displays the source code for the two Delphi examples that
are included in the Chilkat XML download.
Example
#1: Generate XML
This example
generates three different XML files.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, CHILKATXMLLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
XmlFactory1: TXmlFactory;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
xml: IChilkatXml;
begin
xml := XmlFactory1.NewXml();
xml.Tag := 'myTag';
xml.Content := 'This is content';
xml.SaveXML('data1.xml');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
xml: IChilkatXml;
i: Integer;
xml2: IChilkatXml;
begin
xml := XmlFactory1.NewXml();
xml.Tag := 'sample';
for i:= 0 to 100 do
begin
xml2 := xml.NewChild('record','');
xml2.NewChild('first_name','Matt');
xml2.NewChild('last_name','Fausey');
xml2.NewChild('company','Chilkat Software, Inc.');
xml2.NewChild('website','http://www.chilkatsoft.com');
xml2.NewChild('email','fausey@chilkatsoft.com');
end;//
xml.SaveXML('data2.xml');
end;
procedure TForm1.Button3Click(Sender: TObject);
var
xml: IChilkatXml;
i: Integer;
xml2: IChilkatXml;
xml3: IChilkatXml;
begin
xml := XmlFactory1.NewXml();
xml.Tag := 'sample';
for i:= 0 to 100 do
begin
xml2 := xml.NewChild('record','');
xml3 := xml2.NewChild('first_name','Matt');
xml3.AddAttribute('favorite_color','blue');
xml3.AddAttribute('favorite_beverage','coffee');
xml2.NewChild('last_name','Fausey');
xml3 := xml2.NewChild('company','Chilkat Software, Inc.');
xml3.AddAttribute('headquarters','Chicago');
xml2.NewChild('website','http://www.chilkatsoft.com');
xml2.NewChild('email','fausey@chilkatsoft.com');
end;//
xml.SaveXML('data3.xml');
end;
procedure TForm1.FormCreate(Sender: TObject);
var
xml: IChilkatXml;
begin
// Unlock the component.
xml := XmlFactory1.NewXml();
end;
end.
Example
#2: Navigate XML
This example
loads an XML file containing the Shakespeare play "Hamlet"
and demonstrates how to navigate around the file and display some
information. The data file as well as source and project files
are included in the Chilkat XML download.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, CHILKATXMLLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
XmlFactory1: TXmlFactory;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
xml: IChilkatXml;
xml2: IChilkatXml;
xml3: IChilkatXml;
count: Integer;
begin
// Open hamlet.xml
xml := XmlFactory1.NewXml();
xml.LoadXmlFile('hamlet.xml');
// Find the play's title.
xml2 := xml.SearchForTag(nil,'TITLE');
Memo1.Lines.Append('Title: ' + xml2.Content);
Memo1.Lines.Append(' ');
// Find the courtiers group: courtiers.
// and then print out each of them.
xml2 := xml.SearchForContent(nil,'GRPDESCR','*courtier*');
// Move up one level to the parent.
xml2 := xml2.GetParent();
// Step through each tag.
xml2 := xml2.FirstChild();
while xml2 <> nil do
begin
if xml2.Tag = 'PERSONA' then
begin
Memo1.Lines.Append('courtier: ' + xml2.Content);
end;
xml2 := xml2.NextSibling();
end;
Memo1.Lines.Append(' ');
// Find the part in the play containing this phrase:
// "There are more things in heaven and earth" and then display
// the entire passage.
xml2 := xml.SearchAllForContent(nil,'*There are more things in heaven and earth*');
xml2 := xml2.GetParent();
xml2 := xml2.FirstChild();
while xml2 <> nil do
begin
if xml2.Tag = 'LINE' then
begin
Memo1.Lines.Append(xml2.Content);
end;
xml2 := xml2.NextSibling();
end;
Memo1.Lines.Append(' ');
// Count the number of times Hamlet speaks.
xml2 := xml.SearchForContent(nil,'SPEAKER','HAMLET');
count := 0;
while xml2 <> nil do
begin
count := count + 1;
xml3 := xml2.NextSibling();
Memo1.Lines.Append(xml3.Content);
xml2 := xml.SearchForContent(xml2,'SPEAKER','HAMLET');
end;
Memo1.Lines.Append(' ');
Memo1.Lines.Append('Hamlet speaks '+IntToStr(count)+' times');
end;
procedure TForm1.FormCreate(Sender: TObject);
var
xml: IChilkatXml;
begin
// Unlock the component.
xml := XmlFactory1.NewXml();
end;
end.
|