<< Click to Display Table of Contents >> Navigation: ThinBASIC Modules > AppConfig > cAppConfig Class |
cAppConfig class
cAppConfig class is used to handle an application configuration object able to load application parameters from an XML configuration file.
To create a new cAppConfig object, in you script add something like:
Dim MyAppConfig As New cAppConfig
MyAppConfig object variable is now ready be be used.
Format of the XML configuration file
To be used by AppConfig module, a configuration XML file must be created following few mandatory rules:
1.XML file MUST adhere to XML rules. More info at (for example: https://www.w3schools.com/xml/xml_whatis.asp
IMPORTANT: XML files content is case sensitive.
2.XML file MUST have a root node called <AppConfig></AppConfig>
3.inside <AppConfig></AppConfig> there can be any sequence of sibling nodes up to 3 levels
Example of valid XML configuration file:
<?xml version="1.0" encoding="utf-8"?>
<AppConfig>
<!--this is a comment-->
<AppVersion>1.0.0</AppVersion>
<AppData>
<Version>1.0</Version>
<Author_eMail>support@thinbasic.com</Author_eMail>
<Author_WebSite>https://www.thinbasic.com</Author_WebSite>
</AppData>
<AppFiles>
<File1>NameFile1.txt</File1>
<File2>NameFile2.txt</File2>
</AppFiles>
<Buffers>
<Buffer1><![CDATA[some stuff]]></Buffer1> <!-- Comment -->
</Buffers>
</AppConfig>
How to load an XML configuration file or buffer
Once a MyAppConfig object variable is created, programmer needs to load an XML configuration file in this way:
MyAppConfig.Load(FullXMLFilePathName)
This will load and parse the XML file loading all parameters/value pairs inside an internal hash table structure.
How to check if parsing of XML file generated errors
Once file is loaded, it is better to check if any error occurred during parsing.
To achieve this, just use something like the following:
if MyAppConfig.ErrorPresent Then
'---Some error occurred
printl "Error code", MyAppConfig.ErrorCode
printl "Error description", MyAppConfig.ErrorDescription
Else
'---No errors, we can go on
end if
How to get values from parsed XML configuration file
Once file is loaded and parsed, all data inside XML file is stored into an hash table with key/data pairs.
the key under which data is stored is composed by a concatenation of nodes and sibling nodes separated by \
To get values use GetParam method passing the concatenation of the nodes separated by \
Like the following example:
if AppConfig.ErrorPresent Then
'---Some error occurred
printl "Error code", AppConfig.ErrorCode
printl "Error description", AppConfig.ErrorDescription
Else
'---No errors, we can go on
printl "0", AppConfig.GetKey("AppVersion")
printl "1", AppConfig.GetKey("AppData\Version")
printl "2", AppConfig.GetKey("AppFiles\File1")
printl "3", AppConfig.GetKey("AppFiles\File2")
printl "4", AppConfig.GetKey("Buffers\Buffer1")
end if