uses NativeXML;
const
xmlFile = 'C:\Temp\xml_test_files\basic.XML' ; //測試文件
var
xml: TNativeXML;
procedure TForm1.FormCreate(Sender: TObject);
begin
xml := TNativeXML.Create(nil);
xml.LoadFromFile(XMLFile);
end;
{獲取根節點的類名及類的繼承關系}
procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
myClass: TClass;
begin
ShowMessage(XML.Root.ClassName); //TsdElement
str := '';
myClass := XML.Root.ClassType;
while myClass <> nil do
begin
if str <> '' then str := ' > ' + str;
str := myClass.ClassName + str;
myClass := myClass.ClassParent;
end;
ShowMessage(str); //TObject > TPersistent > TDebugPersistent > TXMLNode > TsdContainerNode > TsdElement
end;
{根節點的名稱與內容}
procedure TForm1.Button2Click(Sender: TObject);
var
root: TsdElement;
begin
root := XML.Root;
ShowMessage(root.Name); //名稱
ShowMessage(root.ElementTypeName); //類型名稱
ShowMessage(root.WriteToString); //內容
end;
{其子成員}
procedure TForm1.Button3Click(Sender: TObject);
var
root: TsdElement;
i: Integer;
begin
root := XML.Root;
ShowMessage(IntToStr(root.AttributeCount)); //屬性總數
ShowMessage(IntToStr(root.NodeCount)); //子節點數目
Memo1.Clear;
for i := 0 to root.NodeCount - 1 do //遍歷子節點
begin
Memo1.Lines.Add(root[i].Name + #9 + root[i].Value); // root[i] = root.Nodes[i]
end;
end;