Home arrow Delphi-Kylix arrow Page 3 - Loading an XML Document into the DOM
DELPHI-KYLIX

Loading an XML Document into the DOM


In this article we will look at the code that we used to create a program that enabled us to load an XML document into DOM. This article is the fourth part of a four-part series that covers Delphi, XML, and the DOM.

Author Info:
By: David Web
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
November 24, 2008
TABLE OF CONTENTS:
  1. · Loading an XML Document into the DOM
  2. · Code Explained
  3. · Adding Nodes
  4. · Checking the file

print this article
SEARCH DEVARTICLES

Loading an XML Document into the DOM - Adding Nodes
(Page 3 of 4 )

Then we start to add the nodes. The nodes will contain the text that will describe them. The text is also added:


NText := Anode.NodeName;

if Anode.IsTextElement then

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

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



The attributes are added next, using a loop:


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

begin

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

tv.Items.AddChild(NTNode,

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

end;



The child nodes are then added to the main nodes and the procedure is called again. The procedure itself is recursive:


// add each child node

if Anode.HasChildNodes then

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

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

end;


When the application starts up, it looks in the "MyXMLFiles" folder to see if any XML files are stored there. If so, it will load the names of those files in the treeview control. As you add more and more XML files to your application, you will not want to press the "load" button every time. You might just want to click on the file name to load it into the DOM.

The procedure that is responsible for loading the files at start up is listed below. It starts by searching for XML files in the MyXMLFiles folder. The search is done using the findfirst() function:


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



The root node is created with the text "MyXMLFiles." It is then automatically selected:



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;


By selecting the root, you will be able to add child nodes to it, which is what the next line of code does:


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;



The process is repeated until there are no more child nodes available:



until FindNext(rec) <> 0;


// Must free up resources used by these successful finds

FindClose(rec);

end



If no files have been found, then an appropriate message will be added to the root node:


else begin

with tv1.Items.AddFirst( nil, 'No Files Found' ) do

begin

Selected := true;

end;

end;

end;


For convenience I've added an extra procedure that will help if you should decide to avoid using the load button. This procedure basically enables you to double click on the treeview control that lists all of your XML files, and then automatically adds that file name to the xmldocument component that loads it into the DOM. Below is the code:


procedure TForm1.tv1DblClick(Sender: TObject);

var

path,xmlfile:string;

begin



The code first extracts the path to the application. This is to make sure that the DOM is able to look for the file in the right place later on:


path:=extractfilepath(application.ExeName);


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