你需要自己写一点代码但是很简单; 简单写一个递归程序供你参考procedure XMLToTreeView(XMLNode:IXMLNode;RootNode:TTreeNode;View:TTreeView); var i:integer; node:TTreeNode; begin node:=View.AddChild(RootNode,XMLNode.Name); for i:=0 to XMLNode.childNods.Count-1 do XMLToTree(XMLNode.childNods[i],node,View);end;在主程序中调用XMLToTree(XMLDocument.DocElement,nil,TreeView);我这里没有Delphi编译器,其中可能有些错误.有问题再提出来吧
var Procedure Domtotree(xmlnode:ixmlnode;Treenode:Ttreenode); Procedure Tform1.Domtotree(xmlnode:ixmlnode;Treenode:TTreenode); Var i:Integer; newtreenode:Ttreenode; Nodetext:string; Attrnode:ixmlnode; Begin If not(xmlnode.NodeType=ntElement) Then exit; nodetext:=xmlnode.NodeName; If xmlnode.IsTextElement Then Begin Nodetext:=Nodetext+'='+Xmlnode.NodeValue; memo1.Lines.Add(nodetext); End; NewTreenode:=Treeview1.Items.AddChild(treenode,Nodetext); For i:=0 To xmlnode.AttributeNodes.Count-1 Do Begin Attrnode:=xmlnode.AttributeNodes.Nodes[i]; Treeview1.items.AddChild(newtreenode,'['+Attrnode.NodeName+'='''+Attrnode.Text+''']'); End; If xmlnode.HasChildNodes Then For i:=0 TO xmlnode.ChildNodes.Count-1 Do domtotree(xmlnode.ChildNodes.Nodes[i],newtreenode); End;procedure TForm1.Button1Click(Sender: TObject); var mXmlDoc: TXMLDocument; root:Ixmlnode; parent_node:Ixmlnode; icount:integer; begin If opendialog1.Execute Then Begin xmldocument1.LoadFromFile(opendialog1.filename); treeview1.Items.Clear; domtotree(xmldocument1.DocumentElement,nil); Treeview1.FullExpand; End; //Memo1.Lines.Add(root.ChildNodes.); end;
其实二者都是树结构,只是TreeView的属性固定而XMLNode的属性不固定,子节点不固定而已。
另一种方法是使用XSL将XML转换为文本,然后用TreeView的LoadFromFile之类的方法加载。但此时可能不能完全控件其图标等属性。
简单写一个递归程序供你参考procedure XMLToTreeView(XMLNode:IXMLNode;RootNode:TTreeNode;View:TTreeView);
var
i:integer;
node:TTreeNode;
begin
node:=View.AddChild(RootNode,XMLNode.Name);
for i:=0 to XMLNode.childNods.Count-1 do XMLToTree(XMLNode.childNods[i],node,View);end;在主程序中调用XMLToTree(XMLDocument.DocElement,nil,TreeView);我这里没有Delphi编译器,其中可能有些错误.有问题再提出来吧
<DSTreeRoot text="地球" open="true" href="http://" target="box" treeId="0">
<DSTree text="中国" open="true" href="http://" target="box" treeId="00">
<DSTree text="湖北" open="false" href="http://" target="box" treeId="000">
<DSTree text="武汉" open="false" href="http://" target="box" treeId="0000"/>
<DSTree text="黄石" open="false" href="http://" target="box" treeId="0001"/>
<DSTree text="宜昌" href="http://" icon="earth.gif" target="box" treeId="0123"/>
<DSTree text="十堰" open="false" href="http://十堰" target="box" treeId="0002">
<DSTree text="城区" open="false" href="http://城区" target="box" treeId="00020">
<DSTree text="人民路" open="false" href="http://人民路" target="box" treeId="000200"/>
<DSTree text="公园" open="false" href="http://" target="box" treeId="000201"/>
<DSTree text="电脑城" open="false" href="http://" target="box" treeId="000202"/>
</DSTree>
</DSTree>
</DSTree>
<DSTree text="湖南" open="true" href="http://" target="box" treeId="001">
<DSTree text="岳阳" open="false" href="http://" target="box" treeId="0011"><DSTree text="青海" open="true" href="http://" target="box" treeId="002">
<DSTree text="平安" open="false" href="http://" target="box" treeId="0020"/>
<DSTree text="玉树" open="false" href="http://" target="box" treeId="0021"/>
</DSTree>
</DSTree>
<DSTree text="长沙" open="false" href="http://" target="box" treeId="0010"/>
</DSTree>
</DSTree>
<DSTree text="外国" open="true" treeId="01">
<DSTree text="日本" open="false" href="http://" target="box" treeId="011">
<DSTree text="广岛" href="http://" target="box" treeId="0110" open="false"><DSTree text="东京" href="http://" target="box" treeId="0111"/>
</DSTree>
</DSTree>
<DSTree text="韩国" href="http://" target="box" treeId="010" open="true"/>
<DSTree text="美国" open="false" href="http://" target="box" treeId="012">
<DSTree text="纽约" href="http://" target="box" treeId="0120"/>
<DSTree text="华盛顿" href="http://" target="box" treeId="0121"/>
</DSTree>
</DSTree>
</DSTreeRoot>是怎样生成TreeView呢?
Procedure Domtotree(xmlnode:ixmlnode;Treenode:Ttreenode);
Procedure Tform1.Domtotree(xmlnode:ixmlnode;Treenode:TTreenode);
Var
i:Integer;
newtreenode:Ttreenode;
Nodetext:string;
Attrnode:ixmlnode;
Begin
If not(xmlnode.NodeType=ntElement) Then
exit;
nodetext:=xmlnode.NodeName;
If xmlnode.IsTextElement Then
Begin
Nodetext:=Nodetext+'='+Xmlnode.NodeValue;
memo1.Lines.Add(nodetext);
End;
NewTreenode:=Treeview1.Items.AddChild(treenode,Nodetext);
For i:=0 To xmlnode.AttributeNodes.Count-1 Do
Begin
Attrnode:=xmlnode.AttributeNodes.Nodes[i];
Treeview1.items.AddChild(newtreenode,'['+Attrnode.NodeName+'='''+Attrnode.Text+''']');
End;
If xmlnode.HasChildNodes Then
For i:=0 TO xmlnode.ChildNodes.Count-1 Do
domtotree(xmlnode.ChildNodes.Nodes[i],newtreenode);
End;procedure TForm1.Button1Click(Sender: TObject);
var
mXmlDoc: TXMLDocument;
root:Ixmlnode;
parent_node:Ixmlnode;
icount:integer;
begin
If opendialog1.Execute Then
Begin
xmldocument1.LoadFromFile(opendialog1.filename);
treeview1.Items.Clear;
domtotree(xmldocument1.DocumentElement,nil);
Treeview1.FullExpand;
End;
//Memo1.Lines.Add(root.ChildNodes.);
end;