首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OmniXML将应用程序的设置保存到xml文件中

如何使用OmniXML将应用程序的设置保存到xml文件中
EN

Stack Overflow用户
提问于 2010-01-22 05:50:57
回答 1查看 2.2K关注 0票数 6

我正在考虑将我的应用程序设置保存为可扩展标记语言,而不是使用注册表,但我很难理解和使用OmniXML。

我知道在座的一些人使用并推荐OnmiXML,所以我希望有人能给我一些建议。

我习惯于在TRegistry不存在的情况下使用它来创建一个新的密钥,但我似乎在OmniXML上找不到任何类似的选项。

基本上,我希望做的是保存不同XML级别的设置,如下所示:

代码语言:javascript
复制
<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做同样的事情?像这样的东西:

代码语言:javascript
复制
SetNodeStr('./settings/CheckForUpdates', True);

这将创建“路径”,如果它还不存在。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-01-22 07:27:28

使用OmniXMLPersistent单元是使用OmniXML保存应用程序设置的简单方法。

正如在OmniXML Sample Page中所解释的,您只需定义一个具有已发布属性的对象,然后使用TOmniXMLWriter类将该对象序列化为文件或字符串(使用TOmniXMLReader类加载)。

序列化支持嵌套的对象,因此您可以拥有复杂的结构,例如,您的xml可以由以下对象表示:

代码语言:javascript
复制
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库!

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2113207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档