本文介绍了如何使用OmniXML将应用程序的设置保存在xml文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将应用程序设置保存为xml而不是使用注册表,但是我很难理解和使用OmniXML。

I'm considering saving my application settings as a xml instead of using the registry but I'm having a hard time understanding and using OmniXML.

我知道一些你们中的人使用和推荐OnmiXML,所以我希望有人可以给我一些指针。

I know some of you here use and recomend OnmiXML so I hope someone can give me some pointers.

如果不存在,我习惯于使用TRegistry创建新密钥,但似乎在OmniXML上找不到任何类似的选项。

I'm used to use TRegistry to create a new key if it doesn't exist, but I can't seem to find any similar option on OmniXML.

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

Basically what I wish to do is save settings on different XML levels, something like this:

<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做相同的事情?
类似于:

Now, when first running the program I wont have the xml file so I will need to create all the sub-levels. With the TRegistry it was easy, just call OpenKey(pathtokey, True) and the key will be created if it doesn't exist. Is there any comparable way to do the same thing with OmniXML?Someting like:

SetNodeStr('./settings/CheckForUpdates', True);

如果尚不存在,则会创建路径。

Which would create the "path" if it doesn't already exist.

推荐答案

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

The easy method to save application settings with OmniXML is to use the OmniXMLPersistent unit.

如中所述,您只需定义一个具有已发布属性的对象并使用 TOmniXMLWriter 类将对象序列化为文件或字符串(通过 TOmniXMLReader 类加载)

As explained in OmniXML Sample Page you simply define an object with published properties and with the TOmniXMLWriter class you serialize the object to a file or a string (loading with the TOmniXMLReader class)

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

The serialization supports objects nested, so you could have complex structures, for example your xml could be represented by this objects:

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 ...

That's all.. without writing a single line of xml yourself...

如果您仍然喜欢手动方式,请查看OmniXMLUtils单位或(由OmniXML的作者Primoz Gabrijelcic编写)

If you still prefer the "manual" way, take a look at the OmniXMLUtils units or the Fluent interface to OmniXML (written by Primoz Gabrijelcic, the OmniXML author)

Ah ..公众感谢Primoz的出色表现delphi库!

Ah.. public thanks to Primoz for this excellent delphi library!

这篇关于如何使用OmniXML将应用程序的设置保存在xml文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:37