加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi(win32)序列化库

发布时间:2020-12-15 09:46:16 所属栏目:大数据 来源:网络整理
导读:是否有任何Delphi序列化库能够序列化记录和记录数组而不是类? 解决方法 @Max您可以使用 JEDI中的 TJvAppXMLFileStorage组件来序列化记录或记录数组. 您可以使用名为WriteBinary的过程来存储数据,并使用ReadBinary来读取. 遗憾的是,这个组件没有太多文档,所
是否有任何Delphi序列化库能够序列化记录和记录数组而不是类?

解决方法

@Max您可以使用 JEDI中的 TJvAppXMLFileStorage组件来序列化记录或记录数组.

您可以使用名为WriteBinary的过程来存储数据,并使用ReadBinary来读取.

遗憾的是,这个组件没有太多文档,所以这里有一个非常简单的例子来存储单个记录(对于一个记录数组,你可以轻松地修改这个源代码).

记录结构

type
  MyRecord= record
      Field1 : Integer;
      Field2 : Double;
      Field3 : String[20];
      Field4 : String[20];
  end;

保存记录

Procedure SaveMyRecord(Rec : MyRecord);
var
  MyStore: TJvAppXMLFileStorage;
begin
  MyStore:= TJvAppXMLFileStorage.Create(nil);
  try
    MyStore.FileName:='C:temprecord.xml'; 
    //this component supports store multiples objects to the same file,so you need use an identifier for you particular object,in this case i'm use the Dummy name.
    MyStore.WriteBinary('Dummy',@Rec,sizeof(Rec));
    MyStore.Xml.SaveToFile(MyStore.FileName);
  finally
    MyStore.Free;
  end;
end;

此过程创建这样的XML文件,数据以十六进制格式编码.

<?xml version="1.0" encoding="iso-8859-1"?>
<Configuration>
  <Dummy>84030000000000003333333333331F400D737472696E6720746573742031000000000000000D737472696E672074657374203200000000000000000000000000</Dummy>
</Configuration>

阅读持久数据

Procedure LoadMyRecord(var Rec : MyRecord);
var
  MyStore: TJvAppXMLFileStorage;
begin
  MyStore:= TJvAppXMLFileStorage.Create(nil);
  try
    MyStore.FileName:='C:temprecord.xml';//point to the same file
    MyStore.Xml.LoadFromFile(MyStore.FileName); //load the file
    MyStore.ReadBinary('Dummy',sizeof(Rec));//use the Dummy identifier and pass the record as an pointer
  finally
    MyStore.Free;
  end;
end;

检查这个完整的项目(在Delphi 7中测试)

program ProjectPersistRecord;

{$APPTYPE CONSOLE}

uses
  SysUtils,JvAppXMLStorage;

type
  MyRecord= record
      Field1 : Integer;
      Field2 : Double;
      Field3 : String[20];
      Field4 : String[20];
  end;

Procedure SaveMyRecord(Rec : MyRecord);
var
  MyStore: TJvAppXMLFileStorage;
begin
  MyStore:= TJvAppXMLFileStorage.Create(nil);
  try
    MyStore.FileName:='C:temprecord.xml';
    MyStore.WriteBinary('Dummy',sizeof(Rec));
    MyStore.Xml.SaveToFile(MyStore.FileName);
  finally
    MyStore.Free;
  end;
end;

Procedure LoadMyRecord(var Rec : MyRecord);
var
  MyStore: TJvAppXMLFileStorage;
begin
  MyStore:= TJvAppXMLFileStorage.Create(nil);
  try
    MyStore.FileName:='C:temprecord.xml';
    MyStore.Xml.LoadFromFile(MyStore.FileName);
    MyStore.ReadBinary('Dummy',sizeof(Rec));
  finally
    MyStore.Free;
  end;
end;


Var
    Rec :  MyRecord;
begin
  //Fill the record
  Rec.Field1:=900;
  Rec.Field2:=7.8;
  Rec.Field3:='string test 1';
  Rec.Field4:='string test 2';
  SaveMyRecord(Rec); //save the record
  FillChar(Rec,SizeOf(Rec),#0); //clear the record variable
  LoadMyRecord(Rec);//restire the record data
  //show the loaded data
  Writeln(rec.field1);
  Writeln(rec.field2);
  Writeln(rec.field3);
  Writeln(rec.field4);
  Readln;
end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读