Fandom

Delphi Programming

IXMLNode Interface

2,918pages on
this wiki
Add New Page
Talk0 Share

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.

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.
Stub
This article is a stub.
Please help enhance the Delphi Programming Wiki by expanding it.
Info
see the VCL Documentation Guidelines for an overview on doc pages

UnitEdit

DescriptionEdit

(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

http://delphi.about.com/library/weekly/aa101904a.htm

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 http://delphi.about.com how to advertise: http://delphi.about.com/library/bladvertise.htm free newsletter: http://delphi.about.com/library/blnewsletter.htm forum: http://forums.about.com/ab-delphi/start/

}

interface

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;

XMLDoc.SaveToFile(ChangeFileExt(ParamStr(0),'.XML'));

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;

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;


end.

See AlsoEdit

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

User Comments/TipsEdit

(Please leave your name with your comment.)

Also on Fandom

Random Wiki