Home arrow Delphi-Kylix arrow Page 4 - Delphi and XML
DELPHI-KYLIX

Delphi and XML


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.

Author Info:
By: David Web
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
November 03, 2008
TABLE OF CONTENTS:
  1. · Delphi and XML
  2. · XSL example
  3. · XML Syntax
  4. · Using XML with Delphi

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials