xxxxxxxxxx
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;