Delphi(win32)序列化库 [英] Delphi (win32) serialization libraries
问题描述
@Max是否有任何Delphi序列化库能够序列化记录和数组的记录而不是类?您可以使用 TJvAppXMLFileStorage 组件从 JEDI 序列化记录或一系列记录。
你可以使用名为 WriteBinary
的程序存储数据,并将 ReadBinary
读取。
不幸的是,这个组件没有太多的文档,所以这里有一个非常简单的例子,用于存储单个记录(对于可以轻松修改此源代码的记录数组)。
记录结构
键入
MyRecord = record
Field1:Integer ;
Field2:Double;
Field3:String [20];
Field4:String [20];
结束
保存记录
程序SaveMyRecord(Rec:MyRecord);
var
MyStore:TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\temp\record.xml';
//此组件支持将多个对象存储到同一个文件,因此您需要为特定对象使用标识符,在这种情况下,我使用Dummy名称。
MyStore.WriteBinary('Dummy',@ Rec,sizeof(Rec));
MyStore.Xml.SaveToFile(MyStore.FileName);
finally
MyStore.Free;
结束
结束
此过程创建一个这样的XML文件,数据以十六进制格式编码。 >
<?xml version =1.0encoding =iso-8859-1?>
<配置>
< Dummy> 84030000000000003333333333331F400D737472696E6720746573742031000000000000000D737472696E6720746573742032000000000000000000000000000000< / Dummy>
< / Configuration>
读取持久化数据
过程LoadMyRecord(var Rec:MyRecord);
var
MyStore:TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\temp\record.xml'; //指向同一个文件
MyStore.Xml.LoadFromFile(MyStore.FileName); //加载文件
MyStore.ReadBinary('Dummy',@ Rec,sizeof(Rec)); //使用Dummy标识符并将记录作为指针传递
finally
MyStore。自由;
结束
结束
检查这个完整的项目(在Delphi 7中测试)
程序ProjectPersistRecord;
{$ APPTYPE CONSOLE}
使用
SysUtils,
JvAppXMLStorage;
type
MyRecord = record
Field1:Integer;
Field2:Double;
Field3:String [20];
Field4:String [20];
结束
过程SaveMyRecord(Rec:MyRecord);
var
MyStore:TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\temp\record.xml';
MyStore.WriteBinary('Dummy',@ Rec,sizeof(Rec));
MyStore.Xml.SaveToFile(MyStore.FileName);
finally
MyStore.Free;
结束
结束
过程LoadMyRecord(var Rec:MyRecord);
var
MyStore:TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\temp\record.xml';
MyStore.Xml.LoadFromFile(MyStore.FileName);
MyStore.ReadBinary('Dummy',@ Rec,sizeof(Rec));
finally
MyStore.Free;
结束
结束
Var
Rec:MyRecord;
begin
//填写记录
Rec.Field1:= 900;
Rec.Field2:= 7.8;
Rec.Field3:='string test 1';
Rec.Field4:='string test 2';
SaveMyRecord(Rec); //保存记录
FillChar(Rec,SizeOf(Rec),#0); //清除记录变量
LoadMyRecord(Rec); //重新记录数据
//显示加载的数据
Writeln(rec.field1);
Writeln(rec.field2);
Writeln(rec.field3);
Writeln(rec.field4);
Readln;
结束。
Are there any Delphi serialization libraries that are capable of serializing records and arrays of records instead of classes?
@Max you can use the TJvAppXMLFileStorage component from JEDI to serialize an record or an array of records.
you can use the procedure called WriteBinary
to store the data and ReadBinary
to read.
unfortunately there is not much documentation on this component, so here you have an very simple example for store a single record (for an array of records you can easily modify this source code).
The record structure
type
MyRecord= record
Field1 : Integer;
Field2 : Double;
Field3 : String[20];
Field4 : String[20];
end;
Save an record
Procedure SaveMyRecord(Rec : MyRecord);
var
MyStore: TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\temp\record.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;
this procedure create an XML file like this, the data is encoded in an hexadecimal format.
<?xml version="1.0" encoding="iso-8859-1"?>
<Configuration>
<Dummy>84030000000000003333333333331F400D737472696E6720746573742031000000000000000D737472696E672074657374203200000000000000000000000000</Dummy>
</Configuration>
Read the persisted data
Procedure LoadMyRecord(var Rec : MyRecord);
var
MyStore: TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\temp\record.xml';//point to the same file
MyStore.Xml.LoadFromFile(MyStore.FileName); //load the file
MyStore.ReadBinary('Dummy', @Rec,sizeof(Rec));//use the Dummy identifier and pass the record as an pointer
finally
MyStore.Free;
end;
end;
Check this full project (tested in 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:\temp\record.xml';
MyStore.WriteBinary('Dummy', @Rec,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:\temp\record.xml';
MyStore.Xml.LoadFromFile(MyStore.FileName);
MyStore.ReadBinary('Dummy', @Rec,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.
这篇关于Delphi(win32)序列化库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!