What's a good way to serialize Delphi object tree to XML--using RTTI and not custom code?

Mattias Andersson picture Mattias Andersson · Dec 15, 2008 · Viewed 12.3k times · Source

What's a good way to serialize a Delphi object tree to XML--using RTTI and not custom code?

I would have loved to find that this feature is already built into Delphi, but it doesn't seem to be.

I've found a few components (posted, below) that seem like they might perform this function. Have you used any of them or some other offering? Have you built your own? Am I missing something obvious, in Delphi?

Answer

Andreas Hausladen picture Andreas Hausladen · Dec 15, 2008

You can use the JVCL TJvAppXMLFileStorage component to serialize TPersistent derived classes.

uses
  JvAppXMLStorage;

var
  Storage: TJvAppXMLFileStorage;
begin
  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', MyObject);
    Storage.Xml.SaveToFile('S:\TestFiles\Test.xml');

    Storage.Xml.LoadFromFile('S:\TestFiles\Test.xml');
    Storage.ReadPersistent('', MyObject);
  finally
    Storage.Free;
  end;
end;