xxxxxxxxxx
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, NativeXml{!};
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure XmlEvent(Sender: TObject; ANode: TXmlNode); //TsdXmlNodeEvent 事件
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
xmlFile = 'C:\Temp\xml_test_files\CDATA_section.xml'; //测试文件
{测试 OnNodeNew 事件:开始载入节点时发生}
procedure TForm1.Button1Click(Sender: TObject);
var
xml: TNativeXml;
begin
Memo1.Clear;
xml := TNativeXml.Create(nil);
xml.OnNodeNew := XmlEvent;
xml.LoadFromFile(xmlFile);
xml.Free;
end;
{测试 OnNodeLoaded 事件:节点载入完成时发生}
procedure TForm1.Button2Click(Sender: TObject);
var
xml: TNativeXml;
begin
Memo1.Clear;
xml := TNativeXml.Create(nil);
xml.OnNodeLoaded := XmlEvent;
xml.LoadFromFile(xmlFile);
xml.Free;
end;
{将被调用的事件, OnNodeNew 和 OnNodeLoaded 事件的参数格式是一样的}
procedure TForm1.XmlEvent(Sender: TObject; ANode: TXmlNode);
var
depth: string;
begin
if ANode.ElementType = xeElement then //xeElement 是 TsdElementType 枚举的类型之一
begin
depth := StringOfChar(#9, ANode.TreeDepth); //TreeDepth 是节点深度
Memo1.Lines.Add(depth + ANode.Name);
end;
end;
end.