Home arrow Delphi-Kylix arrow Page 2 - 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 - Code Explained
(Page 2 of 4 )


The aim of the program is to make it easy for us to manipulate XML documents. This process starts by loading or opening up an XML document. Naturally this will be the first step that this code will take.

Below is the procedure that does just that. The first part of this procedure defines an initial path for the application using the application.exename function:


procedure TForm1.Button1Click(Sender: TObject);

begin

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


Then we execute the opendialog component. This component will open up a dialog window that will enable you to choose an XML file to open:


if od.Execute then

begin


Once you've selected a file, you load it into the xmldocument component. The file, up to this point, would have been stored in the opendialog components filename property.


xd.LoadFromFile(od.FileName);


The next line of code clears the treeview control in which we want to load the XML document:


tv.Items.Clear;


Then we call the DOMShow procedure that actually does the job of loading the XML document into the relevant treeview together with its structure:


DOMShow (xd.DocumentElement, nil);

tv.FullExpand;

end;


end;


The DOMShow procedure is at the heart of the entire program. It is responsible for loading, reading and organizing the structure of the XML document. It is also responsible for showing it in the treeview control:


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



First, we define some variables that we are going to need throughout this procedure:



var

I: Integer;

NTNode: TTreeNode;

NText: string;

AttrNode: IXMLNode;






begin

We then skip text nodes and other special cases:

if not (Anode.NodeType = ntElement) then

Exit;




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