This page is intended as a supplement to the official documentation on Delphi programming. CodeGear is in the process of putting the Delphi documentation on the Web. Once they have done so, this page will link to the relevant page in the official documentation.
This article is a stub.
Please help enhance the Delphi Programming Wiki by expanding it.
see the VCL Documentation Guidelines for an overview on doc pages



(Please provide a description in your own words. It is illegal to use the wording from the Delphi Help.)

Technical CommentsEdit

(Known issues / Documentation clarifications / Things to be aware of)

IXMLNode ExamplesEdit

unit Unit1;


Exporting TreeView to XML. Constructing a TreeView from XML

Here's how to store TTreeView component items to XML (preserving the Text and other properties of a tree node) and how to populate a TreeView from an XML file.

Zarko Gajic, BSCS About Guide to Delphi Programming how to advertise: free newsletter: forum:



uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ImgList, ComCtrls, xmldom, XMLIntf, msxmldom, XMLDoc;

type TForm1 = class(TForm) tree: TTreeView; treeImageList: TImageList; treeStateImageList: TImageList; XMLDocument1: TXMLDocument; procedure FormCreate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); private procedure AssignImagesRandomly; public { Public declarations } end;

var Form1: TForm1;

implementation {$R *.dfm}

procedure XML2Tree(tree : TTreeView; XMLDoc : TXMLDocument); var jNode : IXMLNode; asd:String;

procedure ProcessNode(Node : IXMLNode; tn : TTreeNode); var cNode : IXMLNode; begin if Node = nil then Exit; tn := tree.Items.AddChild(tn, Node.Attributes['api']); tn.ImageIndex := 1; //tn.StateIndex := Integer(Node.Attributes['stateIndex']);

cNode := Node.ChildNodes.First; while cNode <> nil do begin ProcessNode(cNode, tn); cNode := cNode.NextSibling; end; end; (*ProcessNode*) begin tree.Items.Clear; XMLDoc.FileName := 'api.XML'; XMLDoc.Active := True;

if XMLDoc.ChildNodes.First = nil then begin ShowMessage('nil'); Exit; end;

jNode := XMLDoc.DocumentElement.ChildNodes.First; asd := jNode.Text;

while jNode <> nil do begin ProcessNode(jNode,nil); jNode := jNode.NextSibling; end;

XMLDoc.Active := False; end;

procedure Tree2XML(tree: TTreeView); var tn : TTreeNode; XMLDoc : TXMLDocument; iNode : IXMLNode;

procedure ProcessTreeItem(tn : TTreeNode; iNode : IXMLNode); var cNode : IXMLNode; begin if (tn = nil) then Exit; cNode := iNode.AddChild('item'); cNode.Attributes['text'] := tn.Text; cNode.Attributes['imageIndex'] := tn.ImageIndex; cNode.Attributes['stateIndex'] := tn.StateIndex;

//child nodes tn := tn.getFirstChild; while tn <> nil do begin ProcessTreeItem(tn, cNode); tn := tn.getNextSibling; end; end; (*ProcessTreeItem*) begin XMLDoc := TXMLDocument.Create(nil); XMLDoc.Active := True; iNode := XMLDoc.AddChild('tree2xml'); iNode.Attributes['app'] := ParamStr(0);

tn := tree.TopItem; while tn <> nil do begin ProcessTreeItem (tn, iNode);

tn := tn.getNextSibling; end;


end; (* Tree2XML *)

procedure TForm1.AssignImagesRandomly; var maxIdx, maxStateIdx: integer; tn : TTreeNode; begin maxIdx := treeImageList.Count; maxStateIdx := treeStateImageList.Count;

Randomize; tn := tree.TopItem; while tn <> nil do begin tn.ImageIndex := Integer(Random(maxIdx)); tn.StateIndex := 1 + Integer(Random(maxStateIdx)); tn := tn.GetNext; end;


procedure TForm1.FormCreate(Sender: TObject); begin tree.FullExpand;

//Randomly assign Image indexes and State indexes if NOT FileExists(ChangeFileExt(ParamStr(0),'.XML')) then AssignImagesRandomly else XML2Tree(tree, XMLDocument1); end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin case MessageDlg('Save tree items to XML?', mtConfirmation, [mbYes, mbNo, mbCancel],0) of mrYes: Tree2XML(tree); mrNo: CanClose := True; mrCancel: CanClose := False; end; end;


See AlsoEdit

(Please provide links to items specifically related to this item.)

User Comments/TipsEdit

(Please leave your name with your comment.)

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.