持久对象在Windows XP / Delphi 7 [英] Persistent Objects in Windows XP/Delphi 7
问题描述
非常感谢。
我很抱歉我的英文。
这里有更多的信息...
TAlarm是一个从TObject下降的类,基本上是。还有10个班级从TAlarm(某些类型的闹钟)下降。 TAlarm有一个名为FParams:TParams的字段,子类只有一个Execute方法。 FParams可以是不同的类型:TAlarmX1_Params,TAlarmX2_Params等等。
您可以继承TPersistent和那么您可以使用TJvAppXMLFileStorage( JVCL )组件来序列化TAlarm类。
保存对象
使用
JvAppXMLStorage;
过程SaveMyObject(MyAlarm:TAlarm)
var
MyStore:TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.WritePersistent('',MyAlarm);
MyStore.Xml.SaveToFile('C:\MyAlarm.xml');
finally
MyStore.Free;
结束
结束
还原对象
使用
JvAppXMLStorage;
过程LoadMyObject(MyAlarm:TAlarm)
var
MyStore:TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\MyAlarm.xml';
MyStore.Xml.LoadFromFile('C:\MyAlarm.xml');
MyStore.ReadPersistent('',MyAlarm);
finally
MyStore.Free;
结束
结束
更新
如果您需要坚持XML文件的多个对象必须为WritePersistent和ReadPersistent方法分配一个路径(唯一ID)。
看到这个例子,
多重持久化
过程SaveMyObjects (MyObjects:TComponent数组);
var
MyStore:TJvAppXMLFileStorage;
i:integer;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
尝试
为i:=低(MyObjects)到高(MyObjects)做
MyStore.WritePersistent(MyObjects [i] .Name,MyObjects [i]); //在这种情况下,我使用组件的name属性。
MyStore.Xml.SaveToFile('C:\Tools\MyAlarm.xml');
finally
MyStore.Free;
结束
结束
保存组件
SaveMyObjects([Button1,Button2,Edit1,Edit2]);
多个LOAD
过程LoadMyObjects(MyObjects:TComponent的数组);
var
MyStore:TJvAppXMLFileStorage;
i:integer;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\Tools\MyAlarm.xml';
MyStore.Xml.LoadFromFile('C:\Tools\MyAlarm.xml');
for i:= Low(MyObjects)to High(MyObjects)do
MyStore.ReadPersistent(MyObjects [i] .Name,MyObjects [i]);
finally
MyStore.Free;
结束
结束
恢复属性
LoadMyObjects([Button1,Button2,Edit1,Edit2]);
加载
另一个选项 过程LoadMyObjectById(Id:String; MyObject:TComponent); //使用对象的id
var
MyStore:TJvAppXMLFileStorage;
i:integer;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\Tools\MyAlarm.xml';
MyStore.Xml.LoadFromFile('C:\Tools\MyAlarm.xml');
MyStore.ReadPersistent(id,MyObject);
finally
MyStore.Free;
结束
结束
您必须以这种方式运行
LoadMyObjectById(Button1.Name,Button1); //再次使用Name属性。
我希望这个例子会很有用;)
I am trying to make an AlarmSystem in Delphi 7, Windows XP. I have to register alarms in a Database (MS SQL Server 2000). But what if the server is down??? Well, I can imagine that I have to persist objects of TAlarm type. So, how can I do this? Maybe inheriting from TComponent??? Please, how can I do this??
Thanks a lot.
I am sorry about my English.
Here you have more info... TAlarm is a class that descends from TObject, basically. There are 10 more classes that descend from TAlarm (some types of alarms). TAlarm has a field named FParams : TParams, and the child classes only have an Execute method. The field FParams can be of different types: TAlarmX1_Params, TAlarmX2_Params, etc, etc, etc.
You can inheriting from TPersistent and then you can use the TJvAppXMLFileStorage (JVCL) component to serialize the TAlarm class.
Save a Object
uses
JvAppXMLStorage;
Procedure SaveMyObject(MyAlarm : TAlarm)
var
MyStore: TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.WritePersistent('', MyAlarm);
MyStore.Xml.SaveToFile('C:\MyAlarm.xml');
finally
MyStore.Free;
end;
end;
Restore a Object
uses
JvAppXMLStorage;
Procedure LoadMyObject(MyAlarm : TAlarm)
var
MyStore: TJvAppXMLFileStorage;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\MyAlarm.xml';
MyStore.Xml.LoadFromFile('C:\MyAlarm.xml');
MyStore.ReadPersistent('', MyAlarm);
finally
MyStore.Free;
end;
end;
UPDATE
If you need to persist more than one object to the XML file you must assign a path (unique id) to the WritePersistent and ReadPersistent methods.
See this example,
Multiple Persist
Procedure SaveMyObjects(MyObjects : Array of TComponent);
var
MyStore: TJvAppXMLFileStorage;
i : integer;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
for i := Low(MyObjects) to High(MyObjects) do
MyStore.WritePersistent(MyObjects[i].Name, MyObjects[i]); //In this case i use the name property of the component.
MyStore.Xml.SaveToFile('C:\Tools\MyAlarm.xml');
finally
MyStore.Free;
end;
end;
to save the components
SaveMyObjects([Button1,Button2,Edit1,Edit2]);
Multiple LOAD
Procedure LoadMyObjects(MyObjects:Array of TComponent);
var
MyStore : TJvAppXMLFileStorage;
i : integer;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\Tools\MyAlarm.xml';
MyStore.Xml.LoadFromFile('C:\Tools\MyAlarm.xml');
for i := Low(MyObjects) to High(MyObjects) do
MyStore.ReadPersistent(MyObjects[i].Name, MyObjects[i]);
finally
MyStore.Free;
end;
end;
To restore the properties
LoadMyObjects([Button1,Button2,Edit1,Edit2]);
Another option to load
Procedure LoadMyObjectById(Id:String;MyObject:TComponent); //using the id of the object
var
MyStore : TJvAppXMLFileStorage;
i : integer;
begin
MyStore:= TJvAppXMLFileStorage.Create(nil);
try
MyStore.FileName:='C:\Tools\MyAlarm.xml';
MyStore.Xml.LoadFromFile('C:\Tools\MyAlarm.xml');
MyStore.ReadPersistent(id, MyObject);
finally
MyStore.Free;
end;
end;
you must run it this way
LoadMyObjectById(Button1.Name,Button1); //Again using the Name property.
I hope this example will be useful ;)
这篇关于持久对象在Windows XP / Delphi 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!