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

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


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;


JVCL is one choice, but if you prefer a small, self-contained library, there's OmniXML (Mozilla Public License 1.1, http://www.omnixml.com/ ). I've used it successfully in several projects, and I find it the simplest XML library to use in Delphi. OmniXML comes with 'OmniXMLPersistent' unit, which does what you need via RTTI, just like the JVCL solution does.

// saving:pers : TPersistent;// SaveToFile is a class method, so no need to instantiate the object:TOmniXMLWriter.SaveToFile( pers, 'd:\path\file.xml', pfAttributes, ofIndent );

pfAttributes means properties will be stored as attributes of XML elements; ofIndent will produce a nicely indented code for readability.

// loading:TOmniXMLWriter.LoadFromFile( pers, 'd:\path\file.xml' ); 


DragonSoft's XML Class Serializer

Link: http://www.dragonsoft.us/delphi_vcl.php

Licence: Licensed under the Mozilla Public Licence ("MPL") version 1.1

Quote: Allows to serialize/deserialize VCL Objects/Components via XML. Store/restore state of the object (published properties). Special classes support - TStrings, TCollection, TPicture. Full process control.