F
fenghuolun
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是一个XML文件,想导入TREEVIEW中,用了下面的代码不能正常显示,另外还想实现点击时,执行URL,哪位能帮一下忙给出解决的方法和代码?
<?xml version="1.0"
encoding="gb2312"?>
<note>
<item text="中国">
<item text="上海"
url=""
tag="100"
/>
<item text="北京 "
url=""
tag="100"
/>
<item text="上海"
url=""
tag="100"
/>
<item text="陕西 "
url=""
tag="100"
/>
</item>
</note>
----------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
MSxml_TLB, ComCtrls, Menus, ExtCtrls, StdCtrls, Dialogs;
type
TxmlNodeWrapper = class
private
FNode: IxmlDOMNode;
protected
public
constructor Create(aNode: IxmlDOMNode);
property Node: IxmlDOMNode read FNode;
end;
type
FieldRecord= record
itemtext: String;
url: String;
tag: integer;
end;
TForm1 = class(TForm)
MainMenu1: TMainMenu;
mmoNodeContent: TMemo;
Panel1: TPanel;
grdAttributes: TStringGrid;
OpenDialog1: TOpenDialog;
File11: TMenuItem;
open12: TMenuItem;
trvStructure: TTreeView;
procedure FormCreate(Sender: TObject);
procedure open12Click(Sender: TObject);
private
FDocument: IxmlDOMDocument;
FFileName: String;
procedure Loadxml;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TxmlNodeWrapper }
constructor TxmlNodeWrapper.Create(aNode: IxmlDOMNode);
begin
inherited Create;
FNode := aNode;
end;
{ TFrmMain }
procedure Tform1.FormCreate(Sender: TObject);
begin
FDocument := CoDOMDocument.Create;
FDocument.async := False;
FDocument.validateOnParse := True;
FDocument.preserveWhiteSpace := True;
FDocument.appendChild(FDocument.createNode(NODE_ELEMENT, 'xmlstart', ''));
end;
procedure TForm1.open12Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
FDocument.load(OpenDialog1.FileName);
FFileName := OpenDialog1.FileName;
Loadxml;
end;
end;
procedure Tform1.Loadxml;
var I: Integer;
procedure EnterNode(const xmlNode: IxmlDOMNode;
TreeNode: TTreeNode);
var
I: Integer;
xmlNodeList: IxmlDOMNodeList;
NewTreeNode: TTreeNode;
begin
NewTreeNode := trvStructure.Items.AddChild(TreeNode, xmlNode.nodeName );
NewTreeNode.Data := TxmlNodeWrapper.Create(xmlNode);
// use xmlNode.childNodes to get all nodes (incl. special types)
xmlNodeList := xmlNode.selectNodes('*');
for I := 0 to Pred(xmlNodeList.length)do
EnterNode(xmlNodeList.item, NewTreeNode);
end;
begin
for I := 0 to trvStructure.Items.Count - 1do
TxmlNodeWrapper(trvStructure.Items.Item.Data).Destroy;
trvStructure.Items.begin
Update;
try
trvStructure.Items.Clear;
EnterNode(FDocument.documentElement, nil);
finally
trvStructure.Items.EndUpdate;
end;
end;
<?xml version="1.0"
encoding="gb2312"?>
<note>
<item text="中国">
<item text="上海"
url=""
tag="100"
/>
<item text="北京 "
url=""
tag="100"
/>
<item text="上海"
url=""
tag="100"
/>
<item text="陕西 "
url=""
tag="100"
/>
</item>
</note>
----------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
MSxml_TLB, ComCtrls, Menus, ExtCtrls, StdCtrls, Dialogs;
type
TxmlNodeWrapper = class
private
FNode: IxmlDOMNode;
protected
public
constructor Create(aNode: IxmlDOMNode);
property Node: IxmlDOMNode read FNode;
end;
type
FieldRecord= record
itemtext: String;
url: String;
tag: integer;
end;
TForm1 = class(TForm)
MainMenu1: TMainMenu;
mmoNodeContent: TMemo;
Panel1: TPanel;
grdAttributes: TStringGrid;
OpenDialog1: TOpenDialog;
File11: TMenuItem;
open12: TMenuItem;
trvStructure: TTreeView;
procedure FormCreate(Sender: TObject);
procedure open12Click(Sender: TObject);
private
FDocument: IxmlDOMDocument;
FFileName: String;
procedure Loadxml;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TxmlNodeWrapper }
constructor TxmlNodeWrapper.Create(aNode: IxmlDOMNode);
begin
inherited Create;
FNode := aNode;
end;
{ TFrmMain }
procedure Tform1.FormCreate(Sender: TObject);
begin
FDocument := CoDOMDocument.Create;
FDocument.async := False;
FDocument.validateOnParse := True;
FDocument.preserveWhiteSpace := True;
FDocument.appendChild(FDocument.createNode(NODE_ELEMENT, 'xmlstart', ''));
end;
procedure TForm1.open12Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
FDocument.load(OpenDialog1.FileName);
FFileName := OpenDialog1.FileName;
Loadxml;
end;
end;
procedure Tform1.Loadxml;
var I: Integer;
procedure EnterNode(const xmlNode: IxmlDOMNode;
TreeNode: TTreeNode);
var
I: Integer;
xmlNodeList: IxmlDOMNodeList;
NewTreeNode: TTreeNode;
begin
NewTreeNode := trvStructure.Items.AddChild(TreeNode, xmlNode.nodeName );
NewTreeNode.Data := TxmlNodeWrapper.Create(xmlNode);
// use xmlNode.childNodes to get all nodes (incl. special types)
xmlNodeList := xmlNode.selectNodes('*');
for I := 0 to Pred(xmlNodeList.length)do
EnterNode(xmlNodeList.item, NewTreeNode);
end;
begin
for I := 0 to trvStructure.Items.Count - 1do
TxmlNodeWrapper(trvStructure.Items.Item.Data).Destroy;
trvStructure.Items.begin
Update;
try
trvStructure.Items.Clear;
EnterNode(FDocument.documentElement, nil);
finally
trvStructure.Items.EndUpdate;
end;
end;