Recent

Author Topic: Problems reading from TXMLConfig  (Read 865 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
Problems reading from TXMLConfig
« on: September 17, 2021, 07:06:08 pm »
Hi,

I have a XML config file which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<CONFIG>
  <Confirmations ExitProgram="0"/>
  <Database StandardDirectory=""/>
</CONFIG>

I now try to load these options at program start:

Code: Text  [Select][+][-]
  1.   function GetInt(const APath: WideString; const ADefault: WideString): LongInt;
  2.   var b: Boolean;
  3.       i: LongInt;
  4.   begin
  5.     b := TryStrToInt(AnsiString(frmOpt.XMLConfig.GetValue(APath, ADefault)),i);
  6.     if (not b) then
  7.       flag := True;
  8.     exit(i);
  9.   end;
  10.  

This works for bla := GetInt('Confirmations/ExitProgram','');

I now try to load the string values with this:

Code: Text  [Select][+][-]
  1.   function GetString(const APath: WideString; const ADefault: WideString): String;
  2.   var s: String;
  3.   begin
  4.     s := AnsiString(frmOpt.XMLConfig.GetValue(APath, ADefault));
  5.     exit(s);
  6.   end;
  7.  

This does NOT work for DbDatabaseDir := GetString('Database/StandardDirectory','');
(It doesn't work either when I omit the AnsiString-Cast...
The debugger says, the variable is NIL.

Any ideas?

Thx,
--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

wp

  • Hero Member
  • *****
  • Posts: 8897
Re: Problems reading from TXMLConfig
« Reply #1 on: September 17, 2021, 10:07:20 pm »
I don't know what's wrong in your code.

The following one, based on yours, is working correctly:
Code: Pascal  [Select][+][-]
  1. const
  2.   xml =
  3.     '<?xml version="1.0" encoding="utf-8"?>' +
  4.     '<CONFIG>'+
  5.       '<Confirmations ExitProgram="12"/>'+
  6.       '<Database StandardDirectory="Test"/>'+
  7.     '</CONFIG>';
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. var
  11.   stream: TStringStream;
  12. begin
  13.   stream := TStringStream.Create(xml);
  14.   try
  15.     XMLConfig1.LoadFromStream(stream);
  16.     Label1.Caption := XMLConfig1.GetValue('Confirmations/ExitProgram', 0).ToString;
  17.     Label2.Caption := XMLConfig1.GetValue('Database/StandardDirectory', '');
  18.   finally
  19.     stream.Free;
  20.   end;
  21. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 4917
Re: Problems reading from TXMLConfig
« Reply #2 on: September 17, 2021, 11:28:25 pm »
Hi,

I have a XML config file which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<CONFIG>
  <Confirmations ExitProgram="0"/>
  <Database StandardDirectory=""/>  ///<<<<<<< you have a blank value entry... !
</CONFIG>

I now try to load these options at program start:

Code: Text  [Select][+][-]
  1.   function GetInt(const APath: WideString; const ADefault: WideString): LongInt;
  2.   var b: Boolean;
  3.       i: LongInt;
  4.   begin
  5.     b := TryStrToInt(AnsiString(frmOpt.XMLConfig.GetValue(APath, ADefault)),i);
  6.     if (not b) then
  7.       flag := True;
  8.     exit(i);
  9.   end;
  10.  

This works for bla := GetInt('Confirmations/ExitProgram','');

I now try to load the string values with this:

Code: Text  [Select][+][-]
  1.   function GetString(const APath: WideString; const ADefault: WideString): String;
  2.   var s: String;
  3.   begin
  4.     s := AnsiString(frmOpt.XMLConfig.GetValue(APath, ADefault));
  5.     exit(s);
  6.   end;
  7.  

This does NOT work for DbDatabaseDir := GetString('Database/StandardDirectory','');
(It doesn't work either when I omit the AnsiString-Cast...
The debugger says, the variable is NIL.

Any ideas?

Thx,
--Dietmar

If you look above you will not there is a blank value entry..
so basically you should test for the return of the string as being NIL which to me is nothing.

If returnString <> '' then....

That's just my observations but I guess it also could be the difference between the compiler versions where one will return a valid pointer to a empty string and this one is just simply returning a NIL string, both are empty.
The only true wisdom is knowing you know nothing

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Problems reading from TXMLConfig
« Reply #3 on: September 18, 2021, 04:53:57 pm »
Yes, but even entries with values in it where read as empty/NIL strings...
But I now also found my second error: The entry names in the XML file seem to be case sensitive, and I had a little "case error" in them :/

Glad I found it, now all works.

Thanks @all!

--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

 

TinyPortal © 2005-2018