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

Delphi Wrapper Classes and XML


In this article we will look at a partial listing of the xmlintf unit and then write a program to demonstrate how to use DOM in Delphi. We began our discussion of this topic in the previous article. This is part three of a four-part series.

Author Info:
By: David Web
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
November 17, 2008
TABLE OF CONTENTS:
  1. · Delphi Wrapper Classes and XML
  2. · XML Properties, Methods, and Interface
  3. · A Demonstration
  4. · Second Part of Code

print this article
SEARCH DEVARTICLES

Delphi Wrapper Classes and XML - Second Part of Code
(Page 4 of 4 )


The second part of the code lists a function that will facilitate the manipulation of the elements listed on the DOM tree:


procedure TForm1.DOMShow(Anode: IXMLNode; TNode: TTreeNode);

var

I: Integer;

NTNode: TTreeNode;

NText: string;

AttrNode: IXMLNode;

begin

// skip text nodes and other special cases

if not (Anode.NodeType = ntElement) then

Exit;

// add the node itself

NText := Anode.NodeName;

if Anode.IsTextElement then

NText := NText + ' = ' + Anode.NodeValue;

NTNode := tv.Items.AddChild(TNode, NText);

// add attributes

for I := 0 to Anode.AttributeNodes.Count - 1 do

begin

AttrNode := Anode.AttributeNodes.Nodes[I];

tv.Items.AddChild(NTNode,

'[' + AttrNode.NodeName + ' = "' + AttrNode.Text + '"]');

end;

// add each child node

if Anode.HasChildNodes then

for I := 0 to Anode.ChildNodes.Count - 1 do

DOMShow(Anode.ChildNodes.Nodes [I], NTNode);

end;


The procedure below reads the names of some XML files and loads them into a treeview control:

procedure TForm1.FormCreate(Sender: TObject);

var

rec:TSearchRec;

begin

// Try to find regular files matching *.xml in the current dir

if FindFirst('MyXMLFiles*.xml', faAnyFile, rec) = 0 then

begin

with tv1.Items.AddFirst(nil, 'MyXMLFiles') 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 tv1.Items.AddChildFirst( tv1.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 tv1.Items.AddFirst( nil, 'No Files Found' ) do

begin

Selected := true;

end;

end;

end;


The procedure below is responsible for loading an XML file into the DOM tree:

procedure TForm1.tv1DblClick(Sender: TObject);

var

path,xmlfile:string;

begin {

path:=extractfilepath(application.ExeName);

if (tv1.Selected.Level <> 0) then begin

fn:=tv1.Selected.Text;

//showmessage(path+fn);

xmlfile:=path+fn;

od.FileName:=xmlfile;

xd.LoadFromFile(od.FileName);

// tv.Items.Clear;

DOMShow(xd.DocumentElement, nil);

tv.FullExpand;

end; }

end;


The procedure below is an extra that makes it easier for the user to select XML files from any directory or location on the computer:


procedure TForm1.Button1Click(Sender: TObject);

begin

od.InitialDir := ExtractFilePath (Application.ExeName);

if od.Execute then

begin

xd.LoadFromFile(od.FileName);

tv.Items.Clear;

DOMShow (xd.DocumentElement, nil);

tv.FullExpand;

end;


end;


end.


And here's the program that the code produces:


In the next article we will work through the code and also make further additions to the program.


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