Extensible Markup Language, or XML, is a simplified version of SGML. It is currently receiving a lot of attention. XML is essentially a markup language, meaning that it uses symbols to describe its own content. It also enables you to identify and organize your information in a more accurate and flexible way. This article is the first in a four-part series that introduces you to using XML with Delphi and the DOM.
// 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.