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.