unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, msxml;
type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
doc: IXMLDOMDocument;
ss: TStrings;
HeaderNode: TTreeNode ;
procedure XmlToNode(AXmlNode:IXMLDOMNode;ParentNode: TTreeNode);
var
i:Integer;
begin
if AXmlNode=nil then Exit;
if AXmlNode.nodeType<>NODE_ELEMENT then EXIT;
if AXmlNode.hasChildNodes then
if AXmlNode.childNodes.length=1 then
begin
if AXmlNode.childNodes[0].hasChildNodes then
begin
XmlToNode(AXmlNode.childNodes[0],TreeView1.Items.AddChild(ParentNode,AXmlNode.childNodes.item[0].baseName));
end else
begin
ParentNode.Text := AXmlNode.nodeName +':'+ AXmlNode.text;
end;
end else
for i:=0 to AXmlNode.childNodes.length-1 do
begin
XmlToNode(AXmlNode.childNodes.item[i],TreeView1.Items.AddChild(ParentNode,AXmlNode.childNodes.item[i].baseName));
end;
ParentNode.Expanded := True;
end;
begin
doc := CoDOMDocument.Create;
if doc=nil then exit;
ss := TStringList.Create;
try
ss.LoadFromFile('xml.xml');
doc.LoadXml(ss.Text);
HeaderNode := TreeView1.Items.Add(nil,'根结点');
XmlToNode(doc.DocumentElement,HeaderNode);
finally
doc := nil;
ss.Free;
end;
end;
end.