Delphi and XML - Using XML with Delphi
(Page 4 of 4 )
Below is some code for a very simple program that enables you to open up and modify an XML document.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
xd: TXMLDocument;
tv: TTreeView;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
od: TOpenDialog;
Memo1: TMemo;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
IMG_NODE_ROOT = 0;
IMG_NODE_CLOSED = 1;
IMG_NODE_OPEN = 2;
IMG_NODE_ROOT2 = 3;
IMG_NODE_CLOSED2 = 4;
IMG_NODE_OPEN2 = 5;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.LoadFromFile('myfeedsnew.xml');
end;
procedure TForm1.FormCreate(Sender: TObject);
var
rec:TSearchRec;
SomeTxtFile : TextFile;
buffer : string;
begin
// Try to find regular files matching Unit1.d* in the current dir
if FindFirst('MyFeeds*.xml', faAnyFile, rec) = 0 then
begin
with tv.Items.AddFirst(nil, 'MyFeeds') do
begin
Selected:=true;
{Set the roots image index}
ImageIndex := IMG_NODE_ROOT;
{Set the roots selected index. The same image is uses
as for the ImageIndex}
SelectedIndex := IMG_NODE_ROOT;
end;
repeat
with tv.Items.AddChildFirst( tv.Selected, rec.Name) do
begin
{Set the image used when the node is not selected}
ImageIndex := IMG_NODE_CLOSED;
{Image used when the node is selected}
SelectedIndex := IMG_NODE_OPEN;
MakeVisible;
end;
until FindNext(rec) <> 0;
// Must free up resources used by these successful finds
FindClose(rec);
end
else begin
with tv.Items.AddFirst( nil, 'No Files Found' ) do
begin
Selected := true;
end;
end;
end;
end.
At this stage, the code is not complicated at all. It simply uses the loadfromfile() function to load an XML file and then displays it in the memo:
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.LoadFromFile('myfeedsnew.xml');
end;
Once the XML file is loaded into the memo, you can make the changes that you want. The onformcreate procedure simply searches the myfeeds directory and then lists all the XML files that it finds in a treeview. It groups them together based on the directory to which they belong:
procedure TForm1.FormCreate(Sender: TObject);
var
rec:TSearchRec;
SomeTxtFile : TextFile;
buffer : string;
begin
// Try to find regular files matching Unit1.d* in the current dir
if FindFirst('MyFeeds*.xml', faAnyFile, rec) = 0 then
begin
with tv.Items.AddFirst(nil, 'MyFeeds') do
begin
Selected:=true;
{Set the roots image index}
ImageIndex := IMG_NODE_ROOT;
{Set the roots selected index. The same image is uses
as for the ImageIndex}
SelectedIndex := IMG_NODE_ROOT;
end;
repeat
with tv.Items.AddChildFirst( tv.Selected, rec.Name) do
begin
{Set the image used when the node is not selected}
ImageIndex := IMG_NODE_CLOSED;
{Image used when the node is selected}
SelectedIndex := IMG_NODE_OPEN;
MakeVisible;
end;
until FindNext(rec) <> 0;
// Must free up resources used by these successful finds
FindClose(rec);
end
else begin
with tv.Items.AddFirst( nil, 'No Files Found' ) do
begin
Selected := true;
end;
end;
end;
end.
In the next article we will discuss how to use DOM with Delphi. We will then improve on this application at that stage.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |