//快速初始化功能树
procedure InitTree;
var
doc: IXMLDOMDocument;
Node, ChildNode: IXMLDOMNode;
NodeList: IXMLDOMNodeList;
i, Index: Integer;
id,mc,parentid,level: string;
List: TStringList;
TreeNode: TTreeNode;
begin
if not FileExists(applicationDir + ‘Menu.xml‘) then
begin
MsgBox(‘请确认文件Menu.xml是否存在!‘);
Exit;
end;
cxTreeView.Items.BeginUpdate;
try
cxTreeView.Items.Clear;
List := TStringList.Create;
doc := CoDOMDocument.Create;
try
List.Sorted := True;
doc.load(applicationDir + Menu.xml‘);
NodeList := doc.selectNodes(‘CKTS/SC/item‘);
for i := 0 to NodeList.Length - 1 do
begin
Node := NodeList.Item[i].Attributes.getNamedItem(‘id‘);
if Node = nil then Break;
id:= Node.text;
Node := NodeList.Item[i].Attributes.getNamedItem(‘mc‘);
if Node = nil then Break;
mc:= Node.text;
Node := NodeList.Item[i].Attributes.getNamedItem(‘parentid‘);
if Node = nil then Break;
parentid:= Node.text;
//Node := NodeList.Item[i].Attributes.getNamedItem(‘level‘);
//if Node = nil then Break;
//level:= Node.text;
if StrToInt(parentid) = 0 then { ParentID=0,顶层节点 }
TreeNode := cxTreeView.Items.AddChild(nil, mc)
else
begin
Index := List.IndexOf(parentid);
TreeNode := cxTreeView.Items.AddChild(TTreeNode(List.Objects[Index]), mc);
TreeNode.ImageIndex:= 0;
end;
List.AddObject(id, TreeNode);
end;
finally
FreeAndNil(List);
doc := nil;
end;
cxTreeView.TopItem.Expand(True);
finally
cxTreeView.Items.EndUpdate;
end;
end;
附 Menu.xml:
<?xml version="1.0" encoding="GBK"?>
<CKTS name="XXX业务清单" version="1.0">
<SC>
<item id=‘1‘ parentid=‘0‘ level="1" mc="aa"></item>
<item id=‘2‘ parentid=‘1‘ level="2" mc="ba"></item>
<item id=‘3‘ parentid=‘1‘ level="2" mc="bb"></item>
<item id=‘4‘ parentid=‘1‘ level="2" mc="bc"></item>
<item id=‘5‘ parentid=‘1‘ level="2" mc="bd"></item>
<item id=‘6‘ parentid=‘1‘ level="2" mc="be"></item>
<item id=‘7‘ parentid=‘2‘ level="3" mc="ca"></item>
<item id=‘8‘ parentid=‘2‘ level="3" mc="cb"></item>
<item id=‘9‘ parentid=‘2‘ level="3" mc="cc"></item>
<item id=‘10‘ parentid=‘3‘ level="3" mc="cd"></item>
<item id=‘11‘ parentid=‘3‘ level="3" mc="ce"></item>
<item id=‘12‘ parentid=‘3‘ level="3" mc="cf"></item>
<item id=‘13‘ parentid=‘3‘ level="3" mc="cg"></item>
<item id=‘14‘ parentid=‘3‘ level="3" mc="ch"></item>
<item id=‘15‘ parentid=‘4‘ level="3" mc="ci"></item>
<item id=‘16‘ parentid=‘4‘ level="3" mc="cj"></item>
<item id=‘17‘ parentid=‘4‘ level="3" mc="ck"></item>
<item id=‘18‘ parentid=‘4‘ level="3" mc="cl"></item>
<item id=‘19‘ parentid=‘5‘ level="3" mc="cm"></item>
<item id=‘20‘ parentid=‘5‘ level="3" mc="cn"></item>
<item id=‘21‘ parentid=‘5‘ level="3" mc="co"></item>
<item id=‘22‘ parentid=‘6‘ level="3" mc="cp"></item>
<item id=‘23‘ parentid=‘6‘ level="3" mc="cq"></item>
<item id=‘24‘ parentid=‘6‘ level="3" mc="cr"></item>
<item id=‘25‘ parentid=‘6‘ level="3" mc="cs"></item>
</SC>
</CKTS>