精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>其他>>XML>>使用递归法将XML装入TREEVIEW的源代码

主题:使用递归法将XML装入TREEVIEW的源代码
发信人: charming(零零发)
整理人: teleme(2003-05-28 17:26:06), 站内信件
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, xmldom, XMLIntf, msxmldom, XMLDoc;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    XMLDocument1: TXMLDocument;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure AddNodes(TreeNode: TTreeNode; XMLNode: IXMLNode);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.AddNodes(TreeNode: TTreeNode; XMLNode: IXMLNode);
var
  i: Integer;
  Node: TTreeNode;
begin
  for i := 0 to XMLNode.ChildNodes.Count - 1 do
  begin
    Node := TreeView1.Items.AddChild(TreeNode, XMLNode.ChildNodes.Nodes[i].NodeName);
    if XMLNode.ChildNodes.Nodes[i].ChildNodes.Count > 0 then AddNodes(Node, XMLNode.ChildNodes.Nodes[i]);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Node, CNode: TTreeNode;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    XMLDocument1.LoadFromFile(OpenDialog1.FileName);
    Node := TreeView1.Items.Add(nil, XMLDocument1.DocumentElement.NodeName);
    AddNodes(Node, XMLDocument1.DocumentElement);
  end;
end;

end.



----
21世纪Linux
大量精品光盘、资讯、软件和朋友等着你!  

广发证券
股市有风险,入市需谨慎

羊城网友周刊       

[关闭][返回]