我正在考虑将我的应用程序设置保存为可扩展标记语言,而不是使用注册表,但我很难理解和使用OmniXML。
我知道在座的一些人使用并推荐OnmiXML,所以我希望有人能给我一些建议。
我习惯于在TRegistry不存在的情况下使用它来创建一个新的密钥,但我似乎在OmniXML上找不到任何类似的选项。
基本上,我希望做的是保存不同XML级别的设置,如下所示:
<ProgramName version="6">
<profiles>
<profile name="Default">
<Item ID="aa" Selected="0" />
<Item ID="bb" Selected="1" />
</profile>
</profiles>
<settings>
<CheckForUpdates>1</CheckForUpdates>
<CheckForUpdatesInterval>1</CheckForUpdatesInterval>
<ShowSplashScreen></ShowSplashScreen>
</settings>
</ProgramName>现在,当我第一次运行程序时,我没有xml文件,所以我需要创建所有的子层。使用TRegistry很容易,只需调用OpenKey(pathtokey,True),如果密钥不存在,就会创建它。有没有类似的方法可以用OmniXML做同样的事情?像这样的东西:
SetNodeStr('./settings/CheckForUpdates', True);这将创建“路径”,如果它还不存在。
发布于 2010-01-22 07:27:28
使用OmniXMLPersistent单元是使用OmniXML保存应用程序设置的简单方法。
正如在OmniXML Sample Page中所解释的,您只需定义一个具有已发布属性的对象,然后使用TOmniXMLWriter类将该对象序列化为文件或字符串(使用TOmniXMLReader类加载)。
序列化支持嵌套的对象,因此您可以拥有复杂的结构,例如,您的xml可以由以下对象表示:
type
TAppProfiles = class(TCollection)
...
end;
TAppProfile = class(TCollectionItem)
...
end;
TAppSettings = class(TPersistent)
private
FCheckForUpdates: Integer;
FCheckForUpdatesInterval: Integer;
FShowSplashScreen: Boolean;
published
property CheckForUpdates: Integer read FCheckForUpdates write FCheckForUpdates;
property CheckForUpdatesInterval: Integer read FCheckForUpdatesInterval write FCheckForUpdatesInterval;
property ShowSplashScreen: Boolean read FShowSplashScreen write FShowSplashScreen;
end;
TAppConfiguration = class(TPersistent)
private
FProfiles: TAppProfiles;
FSettings: TAppSettings;
published
property Profiles: TAppProfiles read FProfiles write FProfiles;
property Settings: TAppSettings read FSettings write FSettings;
end;
//Declare an instance of your configuration object
var
AppConf: TAppConfiguration;
//Create it
AppConf := TAppConfiguration.Create;
//Serialize the object!
TOmniXMLWriter.SaveToFile(AppConf, 'appname.xml', pfNodes, ofIndent);
//And, of course, at the program start read the file into the object
TOmniXMLReader.LoadFromFile(AppConf, 'appname.xml');就这样..。不需要自己编写一行xml ...
如果你仍然喜欢“手动”的方式,看看OmniXMLUtils单元或者Fluent interface to OmniXML (由Primoz Gabrijelcic编写,OmniXML的作者)
啊.。公开感谢Primoz提供了这个优秀的delphi库!
https://stackoverflow.com/questions/2113207
复制相似问题