Delphi 中用 Xml 配置文档生成 Treeview:

Delphi 中用 Xml 配置文档生成 Treeview:
    用递归方法,使用 xml 文档生成 Treeview 树形视图。由于是动态生成,所以可以通过修改 xml 的逻辑来定制 Treeview 的结构, 从而实现了 xml 对 Treeview 的动态配置,而不用修改代码。

 

  1. <?xml version="1.0" encoding="gb2312"?> 
  2. <root topic="频道列表" catalog="none"> 
  3. <channel topic="操作系统" catalog="none"> 
  4. <channel topic="Windows频道" catalog="windows" /> 
  5. <channel topic="DOS频道" catalog="dos" /> 
  6. <channel topic="Linux" catalog="linux" /> 
  7. </channel> 
  8. <channel topic="菜鸟专区" catalog="cainiaozhuanqu" /> 
  9. <channel topic="应用软件" catalog="app" /> 
  10. <channel topic="安全专区" catalog="safe" /> 
  11. <channel topic="代码实验室" catalog="lab" /> 
  12. <BBS topic="电脑学习社区" catalog="none"> 
  13. <subBBS topic="子社区-1" catalog="sub1" /> 
  14. <subBBS topic="子社区-2" catalog="sub2" /> 
  15. </BBS> 
  16. </root>

 

  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ComCtrls, StdCtrls, XMLDoc, XMLIntf;
  6. type
  7.   TForm1 = class(TForm)
  8.     Memo1: TMemo;
  9.     Button1: TButton;
  10.     TreeView1: TTreeView;
  11.     procedure TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
  12.       Shift: TShiftState; X, Y: Integer);
  13.     procedure Button1Click(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.     procedure CreateTreeview(XmlNode: IXMLNode; TreeNode: TTreeNode);
  17.   public
  18.     { Public declarations }
  19.   end;
  20. type
  21.   pRec = ^TData;
  22.   TData = record
  23.     sCatalog: string;
  24.     sReserved: string
  25.   end;
  26. var
  27.   Form1: TForm1;
  28. implementation
  29. {$R *.dfm}
  30. procedure TForm1.CreateTreeview(XmlNode: IXMLNode; TreeNode: TTreeNode);
  31. var
  32.   i: integer;
  33.   ParentTreeNode, CurrentTreeNode: TTreeNode;
  34.   pData: pRec;
  35. begin
  36.   New(pData);
  37.   pData^.sCatalog := XmlNode.AttributeNodes['catalog'].NodeValue;
  38.   CurrentTreeNode := TreeView1.Items.AddChildObject(TreeNode,
  39.     XmlNode.AttributeNodes['topic'].NodeValue, pData); //pointer(...)
  40.   if XmlNode.HasChildNodes then
  41.   begin
  42.     ParentTreeNode := CurrentTreeNode;
  43.     for i := 0 to XmlNode.ChildNodes.Count - 1 do
  44.     begin
  45.       CreateTreeview(XmlNode.ChildNodes[i], ParentTreeNode);
  46.     end;
  47.   end;
  48. end;
  49. procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
  50.   Shift: TShiftState; X, Y: Integer);
  51. var pData: pRec;
  52. begin
  53.   pData := Treeview1.Selected.Data;
  54.   Memo1.Lines.Add(pData^.sCatalog);
  55. end;
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. var
  58.   oXml: TXMLDocument;
  59. begin
  60.   oXml := TXMLDocument.Create(self);
  61.   oXml.FileName := 'PXML.xml';
  62.   oXml.Active := true;
  63.   CreateTreeview(oXml.ChildNodes.FindNode('root'), Treeview1.Items.GetFirstNode);
  64.   Treeview1.FullExpand;
  65.   oXml.Free;
  66. end;
  67. end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值