我正在考虑将我的应用程序设置保存为xml而不是使用注册表,但我很难理解并使用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>
Run Code Online (Sandbox Code Playgroud)
现在,当第一次运行程序时,我没有xml文件,所以我需要创建所有子级别.使用TRegistry很容易,只需调用OpenKey(pathtokey,True),如果它不存在,将创建密钥.有没有类似的方法用OmniXML做同样的事情?喜欢:
SetNodeStr('./settings/CheckForUpdates', True);
Run Code Online (Sandbox Code Playgroud)
如果它还不存在,那将创建"路径".
pao*_*ssi 10
使用OmniXML保存应用程序设置的简单方法是使用OmniXMLPersistent单元.
如OmniXML示例页中所述,您只需定义具有已发布属性的对象,并使用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');
Run Code Online (Sandbox Code Playgroud)
这就是全部..没有自己编写一行xml ...
如果您仍然喜欢"手动"方式,请查看OmniXMLUtils单元或OmniXML的Fluent接口(由OmniXML作者Primoz Gabrijelcic编写)
啊..公众感谢Primoz这个优秀的delphi库!