
您可以使用名为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:\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; 此过程创建这样的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:\temp\record.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:\temp\record.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:\temp\record.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. 总结 以上是内存溢出为你收集整理的Delphi(win32)序列化库全部内容,希望文章能够帮你解决Delphi(win32)序列化库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)