XML文件导入TREEVIEW中并解析(100分)

  • 主题发起人 主题发起人 fenghuolun
  • 开始时间 开始时间
F

fenghuolun

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是一个XML文件,想导入TREEVIEW中,用了下面的代码不能正常显示,另外还想实现点击时,执行URL,哪位能帮一下忙给出解决的方法和代码?
<?xml version=&quot;1.0&quot;
encoding=&quot;gb2312&quot;?>
<note>
<item text=&quot;中国&quot;>
<item text=&quot;上海&quot;
url=&quot;&quot;
tag=&quot;100&quot;
/>
<item text=&quot;北京 &quot;
url=&quot;&quot;
tag=&quot;100&quot;
/>
<item text=&quot;上海&quot;
url=&quot;&quot;
tag=&quot;100&quot;
/>
<item text=&quot;陕西 &quot;
url=&quot;&quot;
tag=&quot;100&quot;
/>
</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;
 
FFileName你给没有没有使用打开文件嘛,怎么看呢?
 
请高手指点一下
 
试着用virtual treeview控件。
http://www.delphi-gems.com/VirtualTreeview/
 
我是想把XML文件中的属性值读出来,并显示出来,请大家帮忙
 
后退
顶部