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

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.
Related Discussions
  • HOW TO CALL A FUNC IN A DLL WRITTEN BY DELPHI IN VC6.0? (2001-01-10 21:36:48)
    Mr williamda: Thank you.I am sorry I can not accept your answer.I specify the "stdcall" for my function and I don't use 'string' type. I can...
  • HOW TO LOAD INI INTO A DLL (2001-01-05 05:43:45)
    unfortunatelly I haven't found anything interesting in the win32api help. I tried different methods. It seems like it is loading the data while...
  • PROBLEMS WITH THE 'LIST OBJECT'... (2001-01-17 23:21:01)
    You must replace the "II" (actually it's #13 + #10) with another character when saving and replace it back when loading. Here's a function which...
  • HOW TO EXECUTE THE CPL FILE ??? (2001-01-19 05:53:24)
    Here's many of them. remember that the arguments may be different for different versions of Windows: See my other post on how to run them......
  • THAT HARD? (2001-01-22 19:54:21)
    ok thanks its working now.. the way I was loading the bmp was wrong.. 16x16 is ok btw
  • OLE (2001-01-22 18:07:14)
    If you want to make something like "personalized letter", when you have got one or more pages of normal text, where you want to put the values from...
  • DOES ANYBODY KNOW HOW TO WORK WITH THE TEXCELAPPLICATION COMPONENT?? (2001-01-23 07:28:30)
    Cool! Thanks
  • INTERNET (2001-01-24 21:26:09)
    Ok, to get the options into a listbox: procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL:...
  • EXECUTING A HOST-COMMAND FROM WITHIN A DELPHI PROGRAM (2001-01-24 01:01:51)
    In Delphi you should use ExecuteFile(FileName, Parameters, Directory, nCmdShow); nCmdShow should be SW_SHOW. To use this procedure, you have to...
  • MAILING A REPORT (2001-01-25 13:12:53)
    Thanks, saving as a .qrp file works fine, but it means when I mail the file to someone I must mail with it an .exe file for the receiver to use to...
Latest News
Submit News Form Past News
Latest Forum Entries
Latest Searches