TXMLDocumentRegisterDocBinding (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses the RegisterDocBinding function.

Class diagram

Code

type
  TSpecXMLNode = class(TXMLNode)
  public
    procedure AfterConstruction; override;
    function GetField: string;
  end;

procedure TSpecXMLNode.AfterConstruction;
begin
  Writeln(TSpecXMLNode.ClassName + ' AfterConstruction.');
end;

function TSpecXMLNode.GetField: string;
begin
  Result := 'FieldValue';
end;

procedure Test_RegisterDocBinding;
const
  LTag = 'TestElement';
var
  LDocument: IXMLDocument;
  LNode: IXMLNode;
begin
  LDocument := TXMLDocument.Create(nil);
  LDocument.Active := True;

  { Create document element. }
  LNode := LDocument.CreateElement(LTag, EmptyStr);
  LDocument.DocumentElement := LNode;

  { Register implementation class.
    Should display 'TSpecXMLNode AfterConstruction'. }
  LDocument.RegisterDocBinding(LTag, TSpecXMLNode);

  { Should display 'FieldValue'. }
  Writeln((LDocument.DocumentElement as TSpecXMLNode).GetField);
end;

Uses