[Previous][Up] Reference for unit 'Laz2_XMLCfg' (#lazutils)

Using TXMLConfig.

What is TXMLConfig

TXMLConfig provides a simple mechanism for storing and retrieving values in an XML file. It is intended for use with configuration setting and option values. It is also used as the ancestor for an RTTI-enabled version which store values for object properties using the facilities provided by Run-Time Type Information (RTTI).

Using TXMLConfig is easier than building DOM nodes, child nodes, and attribute values in a TXMLDocument instance. This ease of use is made possible by its use of path specifiers to describe the hierarchy of elements and attributes in the XML document. See TXMLConfig Path Syntax for a description of the path specifier syntax.

Path specifiers are passed as arguments to the GetValue and SetValue methods which read and write values in the XML document. The methods are overloaded to support the use of String, Integer, Boolean, and TRect data types in the value.

Creating Instances of TXMLConfig

Various constructors are available in the class to create TXMLConfig instances. Specifically, they are the Create, CreateClean, and CreateWithSource methods. CreateClean allows an empty XML document to be created that is ultimately stored in the specified file name. CreateWithSource creates an XML document with content specified in a String argument passed to the constructor.

Use the Filename property to change the file name on the local file system where the XML document is stored. Use the Modified property to determine if the XML content in the document has been altered since it was created or loaded.

Loading and Saving XML Content

If the XML document was created was a destination file name, it can be written to disk using the Flush method. TXMLConfig includes other methods that can be used to read or write the XML content in the document to/from a specific file name, or to a TStream instance. Used the ReadXMLFile and WriteXMLFile methods to read and write XML content to a file. Use ReadFromStream and WriteToSteam to read and write XML content using a TStream instance.

// load the content in an existing configuration file
ACfg := TXMLConfig.Create('/path/to/file.xml');
//create an empty XML configuration file
ACfg := TXMLConfig.CreateClean('/path/to/file.xml');
//create an XML configuration file with the specified content, same as CreateClean
ACfg := TXMLConfig.CreateWithSource('/path/to/file.xml', '<CONFIG></CONFIG>');
// stores changes to disk
ACfg.Flush;
// write XML content to a different file
ACfg.WriteXMLFile('/another/path/to/file.xml');
// write XML content to a stream
AStream := TStream.Create;
ACfg.WriteToSteam(AStream);
ACfg := TXMLConfig.CreateClean('myappconfig.xml');
ACfg.SetValue('settings/backup/path', './backup');
ACfg.SetValue('settings/backup/enabled', True);
ACfg.SetValue('settings/spellcheck/enabled', False);

if ACfg.Modified then ACfg.Flush;
ACfg.Free;

The myappconfig.xml file contains the following:

<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
  <settings>
    <backup path="./backup" enabled="True"/>
    <spellcheck enabled="False"/>
  </settings>
</CONFIG>

See also

TXMLConfig

  

Implements a class used to access and maintain an XML configuration data file.

TRttiXMLConfig

  

Implements an XML configuration file with RTTI support.

TXMLConfig-PathSyntax

  

TXMLConfig Path Syntax.


Version 4.0 Generated 2025-05-03 Home