|
NativeXml Help
|
Add a TButton (Button3) and a TMemo (Memo1) to your form, then include NativeXml in the uses clause. Next, type the code below in the TButton.OnClick event handler.
procedure TForm1.Button3Click(Sender: TObject); var ADoc: TNativeXml; begin Memo1.Lines.Clear; ADoc := TNativeXml.Create; try ADoc.OnNodeNew := DoNodeNew; ADoc.OnNodeLoaded := DoNodeLoaded; ADoc.LoadFromFile(Edit1.Text); finally ADoc.Free; end; end;
function TForm1.Indent(ACount: integer): string;
begin
while ACount > 0 do begin
Result := Result + ' ';
dec(ACount);
end;
end;
procedure TForm1.DoNodeNew(Sender: TObject; Node: TXmlNode);
begin
Memo1.Lines.Add(Format('New : %sName=%s', [Indent(Node.TreeDepth), Node.Name]));
end;
procedure TForm1.DoNodeLoaded(Sender: TObject; Node: TXmlNode);
begin
Memo1.Lines.Add(Format('Loaded: %sName=%s, Value=%s', [Indent(Node.TreeDepth), Node.Name, Node.ValueAsString]));
end;
If you do not want to keep a complete copy of the XML file in memory, for example for huge XML files, you can directly remove the node and free its memory. In that case, add the following code to the OnNodeLoaded event:
procedure TForm1.DoNodeLoaded(Sender: TObject; Node: TXmlNode); begin Memo1.Lines.Add(Format('Loaded: Name=%s, Value=%s', [Node.Name, Node.ValueAsString])); if Node.TreeDepth > 0 then begin Memo1.Lines.Add(Format('Deleted: Name=%s', [Node.Name])); Node.Delete; end; end;