将 Delphi 对象树序列化为 XML 的好方法是什么——使用 RTTI 而不是自定义代码?

发布于 2024-07-09 23:19:34 字数 185 浏览 7 评论 0原文

将 Delphi 对象树序列化为 XML 的好方法是什么——使用 RTTI 而不是自定义代码?

我很想发现这个功能已经内置在 Delphi 中,但似乎没有。

我发现了一些组件(发布在下面),它们似乎可以执行此功能。 您使用过其中任何一个或其他产品吗? 你自己建过吗? 我在德尔福中错过了一些明显的东西吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风月客 2024-07-16 23:19:34

您可以使用 JVCL TJvAppXMLFileStorage 组件来序列化 TPersistent 派生类。

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;

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;
Bonjour°[大白 2024-07-16 23:19:34

JVCL 是一种选择,但如果您更喜欢小型、独立的库,可以选择 OmniXML(Mozilla 公共许可证 1.1,http://www.omnixml.com/)。 我已经在几个项目中成功地使用了它,并且我发现它是在 Delphi 中使用的最简单的 XML 库。 OmniXML 附带“OmniXMLPersistent”单元,它可以通过 RTTI 完成您所需的工作,就像 JVCL 解决方案一样。

// 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 表示属性将被存储为 XML 元素的属性; ofIndent 将生成一个良好缩进的代码以提高可读性。

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

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' ); 
余厌 2024-07-16 23:19:34

DragonSoft 的 XML 类序列化器

链接: http://www .dragonsoft.us/delphi_vcl.php

许可证:根据 Mozilla 公共许可证(“MPL”)版本 1.1 许可

引用:允许序列化/反序列化 VCL 对象/通过 XML 的组件。 存储/恢复对象的状态(已发布的属性)。 特殊类支持 - TStrings、TCollection、TPicture。 全过程控制。

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.

别理我 2024-07-16 23:19:34

Simdesign 的 NativeXml

链接: http://www.simdesign .nl/xml.html

许可证: € 29,95

报价: 原生 Delphi XML 解析器和编写器。 独特功能:直接在 XML 中存储、读取和创建任何 TPersistent 对象(参见示例 5)。 这是通过使用 RTTI(运行时类型信息)迭代所有对象的已发布属性来完成的。 此功能仅适用于 D5 及更高版本。

Simdesign's NativeXml

Link: http://www.simdesign.nl/xml.html

Licence: € 29,95

Quote: A native Delphi XML parser and writer. Unique feature: Store, read and create any TPersistent object to/from XML directly (see Example5). This is done by iterating through all of the objects' published properties by use of RTTI (runtime type information). This feature is only available for D5 and up.

徒留西风 2024-07-16 23:19:34

JVCL 的 TJvTranslator.ComponentToXML

链接: http://sourceforge.net/project/showfiles.php?group_id=45786&package_id=42327

许可证: 根据 Mozilla 公共许可证(“MPL”)版本许可1.1

观察:似乎是在进行递归序列化,但事实上它显然是用于“翻译”的,这让我犹豫不决。

JVCL's TJvTranslator.ComponentToXML

Link: http://sourceforge.net/project/showfiles.php?group_id=45786&package_id=42327

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

Observation: Seems to do recursive serialization, but the fact that it is clearly intended for "Translation" gives me pause.

记忆で 2024-07-16 23:19:34

我使用 SOAP XML 为 D2006 制作了一个序列化器:
http://jankajanos.spaces.live.com/blog/cns! C3E2695FC6F7B0A4!791.entry

但也有 D2009 的通用版本。

I've made a serializer for D2006 using SOAP XML:
http://jankajanos.spaces.live.com/blog/cns!C3E2695FC6F7B0A4!791.entry

But there is a generic edition for D2009 too.

苦行僧 2024-07-16 23:19:34

我已经上传了新版本。 里面有英文注释:
http://janosjanka.spaces.live.com/blog/cns! E5C994C03FC0E370!181.entry
此外,它还可以通过预先注册的类类型来反序列化对象。
这是一件非常有用的事情,因为您可以在不知道类型的情况下反序列化对象。

I've uploaded a new version. It contains english comments:
http://janosjanka.spaces.live.com/blog/cns!E5C994C03FC0E370!181.entry
In addition, it can deserialize an object through pre-registred class types.
This is a very useful thing because you can deserialize objects without know types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文