Delphi 中用 Xml 配置文档生成 Treeview

简介:

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

    xml 文件如下:
 

 
  1. <?xml version=”1.0″ encoding=”gb2312″?> 
  2.   <root topic=”频道列表” catalog=”none”> 
  3.  
  4.     <channel topic=”操作系统” catalog=”none”> 
  5.       <channel topic=”Windows频道” catalog=”windows” /> 
  6.       <channel topic=”DOS频道” catalog=”dos” /> 
  7.       <channel topic=”Linux” catalog=”linux” /> 
  8.     </channel> 
  9.  
  10.     <channel topic=”菜鸟专区” catalog=”cainiaozhuanqu” /> 
  11.  
  12.     <channel topic=”应用软件” catalog=”app” /> 
  13.  
  14.     <channel topic=”安全专区” catalog=”safe” /> 
  15.  
  16.     <channel topic=”代码实验室” catalog=”lab” /> 
  17.  
  18.     <BBS topic=”电脑学习社区” catalog=”none”> 
  19.       <subBBS topic=”子社区-1″ catalog=”sub1″ /> 
  20.       <subBBS topic=”子社区-2″ catalog=”sub2″ /> 
  21.     </BBS> 
  22.  
  23.   </root> 
  24.  
  25.   程序代码如下:  
  26.  
  27.   unit tree_xml;  
  28.  
  29.   interface  
  30.  
  31.   uses  
  32.     Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,  
  33.     Forms, Dialogs, ComCtrls, StdCtrls, XMLDoc, XMLIntf;  
  34.  
  35.     type  
  36.     TForm1 = class(TForm)  
  37.       TreeView1: TTreeView;  
  38.       Memo1: TMemo;  
  39.       Button1: TButton;  
  40.       procedure TreeView1MouseDown(Sender: TObject; Button: TMouseButton;  
  41.                      Shift: TShiftState; X, Y: Integer);  
  42.       procedure Button1Click(Sender: TObject);  
  43.     private  
  44.       procedure CreateTreeview(XmlNode: IXMLNode; TreeNode: TTreeNode);  
  45.       { Private declarations }  
  46.     public  
  47.       { Public declarations }  
  48.     end;  
  49.  
  50.     type  
  51.       pRec = ^TData;  
  52.       TData = record 
  53.         sCatalog: string;  
  54.         sReserved: String  
  55.     end;  
  56.  
  57.   var  
  58.     Form1: TForm1;  
  59.  
  60.   implementation  
  61.   {$R *.dfm}  
  62.  
  63.   procedure TForm1.CreateTreeview(XmlNode: IXMLNode; TreeNode: TTreeNode);  
  64.   var  
  65.     i: integer;  
  66.     ParentTreeNode, CurrentTreeNode: TTreeNode;  
  67.     pData: pRec;  
  68.   begin  
  69.     New(pData);  
  70.     pData^.sCatalog :XmlNode.AttributeNodes[’catalog’].NodeValue;  
  71.     CurrentTreeNode :TreeView1.Items.AddChildObject(TreeNode,  
  72.                  XmlNode.AttributeNodes[’topic’].NodeValue, pData); //pointer(…)  
  73.     if XmlNode.HasChildNodes then  
  74.     begin  
  75.       ParentTreeNode :CurrentTreeNode;  
  76.       for i:=0 to XmlNode.ChildNodes.Count-1 do  
  77.       begin  
  78.         CreateTreeview(XmlNode.ChildNodes[i], ParentTreeNode);  
  79.       end;  
  80.     end;  
  81.   end;  
  82.  
  83.   {——————————————————————}  
  84.   procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;  
  85.     Shift: TShiftState; X, Y: Integer);  
  86.   var pData: pRec;  
  87.   begin  
  88.     pData :Treeview1.Selected.Data;  
  89.     Memo1.Lines.Add(pData^.sCatalog);  
  90.   end;  
  91.  
  92.   procedure TForm1.Button1Click(Sender: TObject);  
  93.   var  
  94.     oXml: TXMLDocument;  
  95.   begin  
  96.     oXml :TXMLDocument.Create(self);  
  97.     oXml.FileName := ‘_Treeview.xml’;  
  98.     oXml.Active:=true;  
  99.     CreateTreeview(oXml.ChildNodes.FindNode(’root’), Treeview1.Items.GetFirstNode);  
  100.     Treeview1.FullExpand; //节点全部展开  
  101.     oXml.Free;  
  102.   end;  
  103.  
  104.   end.  

 

  注意程序中 Treeview 的 TreeView1.Items.AddChildObject 方法,其最后一个参数用来保存该节点的相关数据,是一个指针类型的数据,使用时要格外小心。本例中,先定义一个记录类型,再定义一个指针指向它,然后作为 AddChildObject 的最后一个参数。记录类型可以保存节点的很多相关参数,本例中只用到了一个,实际使用时可以任意扩充。











本文转自网眼51CTO博客,原文链接:http://blog.51cto.com/itwatch/286535,如需转载请自行联系原作者

相关文章
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
13 1
|
5月前
|
缓存
web.xml中配置:通用的用户登录过滤器(SessionFilter)
web.xml中配置:通用的用户登录过滤器(SessionFilter)
|
4月前
|
存储 Java 测试技术
JAVA-MAVEN初学者教程(配置、pom.xml、依赖管理等)
JAVA-MAVEN初学者教程(配置、pom.xml、依赖管理等)
236 0
|
1月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
10 0
|
1月前
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
7 0
|
1月前
|
XML Java Apache
Apache Flink自定义 logback xml配置
Apache Flink自定义 logback xml配置
152 0
|
2月前
|
Java
logback配置,命名为logback-spring.xml
logback配置,命名为logback-spring.xml
|
2月前
|
XML Java 测试技术
【SpringBoot】基于 Maven 的 pom.xml 配置详解
【SpringBoot】基于 Maven 的 pom.xml 配置详解
229 0
【SpringBoot】基于 Maven 的 pom.xml 配置详解
|
2月前
log4j2.xml的日志打印配置
log4j2.xml的日志打印配置
30 0