How to use OmniXML to save settings of an application on a xml file

前端 未结 1 610
忘了有多久
忘了有多久 2021-02-04 18:22

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.

I know some of you he

相关标签:
1条回答
  • 2021-02-04 19:00

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

    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)

    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');
    

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

    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.. public thanks to Primoz for this excellent delphi library!

    0 讨论(0)
提交回复
热议问题