Delphi-Kylix
  Home arrow Delphi-Kylix arrow Loading an XML Document into the DOM
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Loading an XML Document into the DOM
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2008-11-24

    Table of Contents:
  • Loading an XML Document into the DOM
  • Code Explained
  • Adding Nodes
  • Checking the file

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Loading an XML Document into the DOM


    (Page 1 of 4 )

    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.

    The Code


    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;

    Button4: TButton;

    od: TOpenDialog;

    tv1: TTreeView;

    procedure Button4Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure tv1DblClick(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure TForm1.Button2Click(Sender: TObject);


    private

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

    { Private declarations }

    public

    fn:string;

    { 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.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;


    procedure TForm1.Button2Click(Sender: TObject);

    begin

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


    end;


    procedure TForm1.Button3Click(Sender: TObject);

    begin

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

    end;


    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;


    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;


    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.

    More Delphi-Kylix Articles
    More By David Web


     

    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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT