unit DemoFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
Winapi.msxml;
type
TDemoForm =
class(TForm)
Button: TButton;
Memo: TMemo;
procedure ButtonClick(Sender: TObject);
end;
var
DemoForm: TDemoForm;
implementation
{$R *.dfm}
type
TDemoClass =
class(Tobject)
SomeInteger: Integer;
SomeString:
String;
DemoClass: TDemoClass;
procedure Serialize(cnth: ISAXContentHandler);
procedure Deserialize(cnth: ISAXContentHandler);
destructor Destroy;
override;
end;
var
sEmpty: WideString;
function StartElement(cnth: ISAXContentHandler;
name:
String): Boolean;
var
a: ISAXAttributes;
sElement: WideString;
iLength: Integer;
begin
a := CoSaxAttributes.Create
as ISAXAttributes;
sElement :=
name;
cnth.startElement(Word(sEmpty[1]), 0, Word(sEmpty[1]), 0, Word(sElement[1]), Length(sElement), a);
end;
function EndElement(cnth: ISAXContentHandler;
name:
String): Boolean;
var
sElement: WideString;
begin
sElement :=
name;
cnth.endElement(Word(sEmpty[1]), 0, Word(sEmpty[1]), 0, Word(sElement[1]), Length(sElement));
end;
function WriteElement(cnth: ISAXContentHandler;
name, content:
String): Boolean;
var
a: ISAXAttributes;
sElement, sContent: WideString;
begin
sElement :=
name;
sContent := content;
cnth.startElement(Word(sEmpty[1]), 0, Word(sEmpty[1]), 0, Word(sElement[1]), Length(sElement), a);
cnth.characters(Word(sContent[1]), Length(sContent));
cnth.endElement(Word(sEmpty[1]), 0, Word(sEmpty[1]), 0, Word(sElement[1]), Length(sElement));
end;
destructor TDemoClass.Destroy;
begin
DemoClass.Free;
inherited;
end;
procedure TDemoClass.Serialize(cnth: ISAXContentHandler);
var
sElement, sContent: WideString;
begin
StartElement(cnth, '
DemoClass');
WriteElement(cnth, '
SomeInteger', IntToStr(SomeInteger));
WriteElement(cnth, '
SomeString', SomeString);
if Assigned(DemoClass)
then
DemoClass.Serialize(cnth);
EndElement(cnth, '
DemoClass');
end;
procedure TDemoClass.Deserialize(cnth: ISAXContentHandler);
begin
// to do
end;
procedure TDemoForm.ButtonClick(Sender: TObject);
var
wrt: IMXWriter;
cnth: ISAXContentHandler;
dc: TDemoClass;
begin
dc := TDemoClass.Create;
dc.SomeInteger := 42;
dc.SomeString := '
Good Omens';
dc.DemoClass := TDemoClass.Create;
dc.DemoClass.SomeInteger := 17;
dc.DemoClass.SomeString := '
marabu';
wrt := CoMXXMLWriter.Create;
cnth := wrt
as ISAXContentHandler;
cnth.startDocument;
dc.Serialize(cnth);
cnth.endDocument;
Memo.Text := VarToStr(wrt.output);
dc.Free;
end;
initialization
sEmpty := '
';
end.