Home arrow Delphi-Kylix arrow Page 4 - 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 - Checking the file
(Page 4 of 4 )

Then we check to see if the user has selected a file. If a file name has been selected, then we need to make sure that it is not the root. The root is located at level zero(0), so we check to make sure that the selected level is not zero:


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


Once we've determined that the selected level is not zero, we then store the name of the selected file in a global variable called fn:


fn:=tv1.Selected.Text;"



Now we combine the previously extracted path and the stored filename and feed that to the xmldocument:


xmlfile:=path+fn;

od.FileName:=xmlfile;

xd.LoadFromFile(od.FileName);



Next we need to add the information contained in the XML document to the treeview. This job will be done by the DOMShow procedure:



tv.Items.Clear;

DOMShow(xd.DocumentElement, nil);

tv.FullExpand;

end;

end;


Once a document is loaded into the XMLDocument component, you can access specific bits of it by using the various methods that are available to the component. For example, to fetch the value of any attribute, write the following:


Xmldocument1.documentelement.attributes['text']


In fact, let's add a procedure to check to see if we can get a value by checking for a node number. Add a button; call it "element check" and add the following code:


procedure TForm1.Button2Click(Sender: TObject);

begin

memo1.Lines.Add(xd.DocumentElement.AttributeNodes.Nodes[0].NodeValue);


end;


To access the nodes you can use something similar and take advantage of the child values array. This is a Delphi extension to the DOM, which allows you to pass as a parameter either the name of the element or its numeric position. Let's add another procedure to demonstrate this:


procedure TForm1.Button3Click(Sender: TObject);

begin

ShowMessage(xd.DocumentElement.ChildNodes.Nodes[0].ChildValues['title']);

end;


In the above code, I will get the title of the child node on the root. This is dependent on what document you have loaded.


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