Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-11

How is XMLConfig supposed to work?

12 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frederic Da Vitoria

Hello,

I am trying to use XmlConfig to store simple configuration data, but I can't make it create a sensible Xml file. This is not really important in my situation, as XmlConfig is able to correctly read back the strange data it wrote, but frustrating. I searched the web but could not find any example. So can anyone offer a working example of creating a xml file with XmlConfig?

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/46f4ec23/attachment.html>
waldo kitty
to Frederic Da Vitoria
Frederic Da Vitoria wrote 7 lines
Hello, I am trying to use XmlConfig to store simple configuration data, but I can't make it create a sensible Xml file. This is not really important in my situation, as XmlConfig is able to correctly read back the strange data it wrote, but frustrating. I searched the web but could not find any example. So can anyone offer a working example of creating a xml file with XmlConfig?

can you provide a sample of what is being created and what you are expecting to actually see?

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Frederic Da Vitoria
to leledumbo

2013/11/27 leledumbo

>From http://wiki.lazarus.freepascal.org/xmlconf, looks like simply setting FileName will do.

Yes, that part works. I dislike the way it works (Implicit saving does not seem very Pascal-ish to me), but it does save a file. My issues are rather with the xml, but I'll rather answer to waldo kitty here

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/c73966bd/attachment.html>
Sven Barth
to Frederic Da Vitoria
Frederic Da Vitoria wrote 10 lines
2013/11/27 leledumbo < <mailto:>> >From http://wiki.lazarus.freepascal.org/xmlconf, looks like simply setting FileName will do. Yes, that part works. I dislike the way it works (Implicit saving does not seem very Pascal-ish to me), but it does save a file. My issues are rather with the xml, but I'll rather answer to waldo kitty here

TIniFile and TMemIniFile do an implicit save as well... (the former during each .Write* the latter during destruction)

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/ea97a2a3/attachment-0001.html>

Frederic Da Vitoria
to waldo kitty: can you provide a sample of what is being created and what you are expecting to actually…

2013/11/27 waldo kitty

Show quoted text 16 lines
On 11/27/2013 6:50 AM, Frederic Da Vitoria wrote: > Hello, > > I am trying to use XmlConfig to store simple configuration data, but I > can't > make it create a sensible Xml file. This is not really important in my > situation, as XmlConfig is able to correctly read back the strange data it > wrote, but frustrating. I searched the web but could not find any > example. So > can anyone offer a working example of creating a xml file with XmlConfig? > can you provide a sample of what is being created and what you are expecting to actually see?

I'm trying something like (just the minimum code, but I can give the complete sources if it makes things easier):

procedure TForm1.Button1Click(Sender: TObject);
const FILE_NAME = 'pXmlConfig.xml' ;
begin
  XmlConfig1.Filename := FILE_NAME ;
//  XmlConfig1.OpenKey ('/CONFIG') ;
  XmlConfig1.SetValue ('/CONFIG/L5', 'xx') ;
  XmlConfig1.SetValue ('/L1/L2', 'aa') ;
  XmlConfig1.SetValue ('/L1/L3', 'bb') ;
  XmlConfig1.SetValue ('/L1/L2', 'cc') ;
  XmlConfig1.SetValue ('/L8', 'dd') ;
//  XmlConfig1.CloseKey ;
  XmlConfig1.Flush
end;

(No, it does not make any sense, just me trying to figure out how it works.)

And here is the result:

<?xml version="1.0" encoding="utf-8"?>
<CONFIG L8="dd">
<CONFIG L5="xx"/>
<L1 L2="cc" L3="bb"/>
</CONFIG>

The only thing which worked as I wanted is that SetValue ('/L1/L2', 'aa') is overwritten by SetValue ('/L1/L2', 'cc')

- How do I create third level keys? (or is XmlConfig able to do more than 2 levels?)
- Why does XmlConfig create L2, L3 and L5 as attributes ("/" usually is a level separator, is it not) ?
- More generally, is XmlConfig suited to do more than a simple list (which would be even less than a .ini can do)?

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/38a73b5f/attachment.html>
Frederic Da Vitoria
to Sven Barth: TIniFile and TMemIniFile do an implicit save as well... (the former during each .Write*…

2013/11/27 Sven Barth

Show quoted text 19 lines
Am 27.11.2013 16:30, schrieb Frederic Da Vitoria: 2013/11/27 leledumbo > >From http://wiki.lazarus.freepascal.org/xmlconf, looks like simply > setting > FileName will do. > Yes, that part works. I dislike the way it works (Implicit saving does not seem very Pascal-ish to me), but it does save a file. My issues are rather with the xml, but I'll rather answer to waldo kitty here TIniFile and TMemIniFile do an implicit save as well... (the former during each .Write* the latter during destruction) Regards, Sven

Yes. But my Pascal reference is Assign - Reset/Rewrite, Close. I still have difficulties appending "File" after Assign and Close :-P Now that I think of it, I never tried to do a new AssignFile after the Reset/Rewrite step, but I would be much surprised if it flushed and closed the first file handle. Anyhow, this is not a real problem as long as it is documented, and the effect of changing the name is documented in the wiki.

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/279af0cf/attachment.html>
Frederic Da Vitoria
to Frederic Da Vitoria: 2013/11/27 waldo kitty I'm trying something like (just the minimum code, but I can give…

2013/11/27 Frederic Da Vitoria

Show quoted text 58 lines
2013/11/27 waldo kitty > On 11/27/2013 6:50 AM, Frederic Da Vitoria wrote: > >> Hello, >> >> I am trying to use XmlConfig to store simple configuration data, but I >> can't >> make it create a sensible Xml file. This is not really important in my >> situation, as XmlConfig is able to correctly read back the strange data >> it >> wrote, but frustrating. I searched the web but could not find any >> example. So >> can anyone offer a working example of creating a xml file with XmlConfig? >> > > can you provide a sample of what is being created and what you are > expecting to actually see? > I'm trying something like (just the minimum code, but I can give the complete sources if it makes things easier): procedure TForm1.Button1Click(Sender: TObject); const FILE_NAME = 'pXmlConfig.xml' ; begin XmlConfig1.Filename := FILE_NAME ; // XmlConfig1.OpenKey ('/CONFIG') ; XmlConfig1.SetValue ('/CONFIG/L5', 'xx') ; XmlConfig1.SetValue ('/L1/L2', 'aa') ; XmlConfig1.SetValue ('/L1/L3', 'bb') ; XmlConfig1.SetValue ('/L1/L2', 'cc') ; XmlConfig1.SetValue ('/L8', 'dd') ; // XmlConfig1.CloseKey ; XmlConfig1.Flush end; (No, it does not make any sense, just me trying to figure out how it works.) And here is the result: <?xml version="1.0" encoding="utf-8"?> <CONFIG L8="dd"> <CONFIG L5="xx"/> <L1 L2="cc" L3="bb"/> </CONFIG> The only thing which worked as I wanted is that SetValue ('/L1/L2', 'aa') is overwritten by SetValue ('/L1/L2', 'cc') - How do I create third level keys? (or is XmlConfig able to do more than 2 levels?) - Why does XmlConfig create L2, L3 and L5 as attributes ("/" usually is a level separator, is it not) ? - More generally, is XmlConfig suited to do more than a simple list (which would be even less than a .ini can do)?

Now that I am thinking of it, I may be expecting too much. If XmlConfig handles more than 2 levels, then why stop to 3? And then, how would XmlConfig be different from a full Xml Implementation?

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/10bf2372/attachment.html>
Sven Barth
to Frederic Da Vitoria: 2013/11/27 Sven Barth Yes. But my Pascal reference is Assign - Reset/Rewrite, Close. I…
Frederic Da Vitoria wrote 24 lines
2013/11/27 Sven Barth < <mailto:>> Am 27.11.2013 16:30, schrieb Frederic Da Vitoria: > 2013/11/27 leledumbo < > <mailto:>> > > >From http://wiki.lazarus.freepascal.org/xmlconf, looks like > simply setting > FileName will do. > > Yes, that part works. I dislike the way it works (Implicit saving > does not seem very Pascal-ish to me), but it does save a file. My > issues are rather with the xml, but I'll rather answer to waldo > kitty here TIniFile and TMemIniFile do an implicit save as well... (the former during each .Write* the latter during destruction) Regards, Sven Yes. But my Pascal reference is Assign - Reset/Rewrite, Close. I still have difficulties appending "File" after Assign and Close :-P

But XmlConfig is AFAIK a spiritual "successor" to TIniFile which is why I brought it up.

Now that I think of it, I never tried to do a new AssignFile after the Reset/Rewrite step, but I would be much surprised if it flushed and closed the first file handle.

It won't flush or close the file, but if you haven't copied the Textfile or File variable somewhere else then you'll lose the reference to the file. It will be closed when the application is terminated, but everything that was not flushed until the Assign will stay in the buffer and won't be in the file... Of course this problem is a bit theoretical and is not really related to your problem :)

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/7261cc79/attachment-0001.html>

Mattias Gaertner
to Frederic Da Vitoria: 2013/11/27 Frederic Da Vitoria Now that I am thinking of it, I may be expecting too much.…
Show quoted text 52 lines
2013/11/27 Frederic Da Vitoria > 2013/11/27 waldo kitty > >> On 11/27/2013 6:50 AM, Frederic Da Vitoria wrote: >> >>> Hello, >>> >>> I am trying to use XmlConfig to store simple configuration data, but I >>> can't >>> make it create a sensible Xml file. This is not really important in my >>> situation, as XmlConfig is able to correctly read back the strange data >>> it >>> wrote, but frustrating. I searched the web but could not find any >>> example. So >>> can anyone offer a working example of creating a xml file with XmlConfig? >>> >> >> can you provide a sample of what is being created and what you are >> expecting to actually see? >> > > I'm trying something like (just the minimum code, but I can give the > complete sources if it makes things easier): > > procedure TForm1.Button1Click(Sender: TObject); > const FILE_NAME = 'pXmlConfig.xml' ; > begin > XmlConfig1.Filename := FILE_NAME ; > // XmlConfig1.OpenKey ('/CONFIG') ; > XmlConfig1.SetValue ('/CONFIG/L5', 'xx') ; > XmlConfig1.SetValue ('/L1/L2', 'aa') ; > XmlConfig1.SetValue ('/L1/L3', 'bb') ; > XmlConfig1.SetValue ('/L1/L2', 'cc') ; > XmlConfig1.SetValue ('/L8', 'dd') ; > // XmlConfig1.CloseKey ; > XmlConfig1.Flush > end; > > (No, it does not make any sense, just me trying to figure out how it > works.) > > And here is the result: > > <?xml version="1.0" encoding="utf-8"?> > <CONFIG L8="dd"> > <CONFIG L5="xx"/> > <L1 L2="cc" L3="bb"/> > </CONFIG> > > The only thing which worked as I wanted is that SetValue ('/L1/L2', 'aa') > is overwritten by SetValue ('/L1/L2', 'cc')

The empty root '/' at the beginning is not needed.

> - How do I create third level keys? (or is XmlConfig able to do more than > 2 levels?)
cfg.SetValue('/L1/L2/L3','value');
> - Why does XmlConfig create L2, L3 and L5 as attributes ("/" usually is a > level separator, is it not) ?

TXMLConfig is a simple wrapper to store name/value pairs in xml. You can access the full XML via cfg.Doc.

> - More generally, is XmlConfig suited to do more than a simple list

You can store lists, trees, graphs as name/value pairs.

> (which would be even less than a .ini can do)?

If you only use TXMLConfig and not the Doc then it can do the same as a TIniFile. Of course there are speed and size differences.

Now that I am thinking of it, I may be expecting too much. If XmlConfig handles more than 2 levels, then why stop to 3? And then, how would XmlConfig be different from a full Xml Implementation?

TXMlConfig uses a pretty complete XML parser/writer.

Mattias

Frederic Da Vitoria
to Sven Barth: But XmlConfig is AFAIK a spiritual "successor" to TIniFile which is why I brought it up.…

2013/11/27 Sven Barth

Show quoted text 30 lines
Am 27.11.2013 16:53, schrieb Frederic Da Vitoria: 2013/11/27 Sven Barth > Am 27.11.2013 16:30, schrieb Frederic Da Vitoria: > > 2013/11/27 leledumbo > >> >From http://wiki.lazarus.freepascal.org/xmlconf, looks like simply >> setting >> FileName will do. >> > > Yes, that part works. I dislike the way it works (Implicit saving does > not seem very Pascal-ish to me), but it does save a file. My issues are > rather with the xml, but I'll rather answer to waldo kitty here > > TIniFile and TMemIniFile do an implicit save as well... (the former > during each .Write* the latter during destruction) > > Regards, > Sven > Yes. But my Pascal reference is Assign - Reset/Rewrite, Close. I still have difficulties appending "File" after Assign and Close :-P But XmlConfig is AFAIK a spiritual "successor" to TIniFile which is why I brought it up.

Ah, OK.

Show quoted text 11 lines
Now that I think of it, I never tried to do a new AssignFile after the Reset/Rewrite step, but I would be much surprised if it flushed and closed the first file handle. It won't flush or close the file, but if you haven't copied the Textfile or File variable somewhere else then you'll lose the reference to the file. It will be closed when the application is terminated, but everything that was not flushed until the Assign will stay in the buffer and won't be in the file... Of course this problem is a bit theoretical and is not really related to your problem :)

Absolutely :-)

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/8d97ea3e/attachment.html>
Frederic Da Vitoria
to Mattias Gaertner: On Wed, 27 Nov 2013 16:54:45 +0100 The empty root '/' at the beginning is not needed.…

2013/11/27 Mattias Gaertner

Show quoted text 96 lines
On Wed, 27 Nov 2013 16:54:45 +0100 Frederic Da Vitoria wrote: > 2013/11/27 Frederic Da Vitoria > > > 2013/11/27 waldo kitty > > > >> On 11/27/2013 6:50 AM, Frederic Da Vitoria wrote: > >> > >>> Hello, > >>> > >>> I am trying to use XmlConfig to store simple configuration data, but I > >>> can't > >>> make it create a sensible Xml file. This is not really important in my > >>> situation, as XmlConfig is able to correctly read back the strange data > >>> it > >>> wrote, but frustrating. I searched the web but could not find any > >>> example. So > >>> can anyone offer a working example of creating a xml file with XmlConfig? > >>> > >> > >> can you provide a sample of what is being created and what you are > >> expecting to actually see? > >> > > > > I'm trying something like (just the minimum code, but I can give the > > complete sources if it makes things easier): > > > > procedure TForm1.Button1Click(Sender: TObject); > > const FILE_NAME = 'pXmlConfig.xml' ; > > begin > > XmlConfig1.Filename := FILE_NAME ; > > // XmlConfig1.OpenKey ('/CONFIG') ; > > XmlConfig1.SetValue ('/CONFIG/L5', 'xx') ; > > XmlConfig1.SetValue ('/L1/L2', 'aa') ; > > XmlConfig1.SetValue ('/L1/L3', 'bb') ; > > XmlConfig1.SetValue ('/L1/L2', 'cc') ; > > XmlConfig1.SetValue ('/L8', 'dd') ; > > // XmlConfig1.CloseKey ; > > XmlConfig1.Flush > > end; > > > > (No, it does not make any sense, just me trying to figure out how it > > works.) > > > > And here is the result: > > > > <?xml version="1.0" encoding="utf-8"?> > > <CONFIG L8="dd"> > > <CONFIG L5="xx"/> > > <L1 L2="cc" L3="bb"/> > > </CONFIG> > > > > The only thing which worked as I wanted is that SetValue ('/L1/L2', 'aa') > > is overwritten by SetValue ('/L1/L2', 'cc') The empty root '/' at the beginning is not needed. > > - How do I create third level keys? (or is XmlConfig able to do more than > > 2 levels?) cfg.SetValue('/L1/L2/L3','value'); > > - Why does XmlConfig create L2, L3 and L5 as attributes ("/" usually is a > > level separator, is it not) ? TXMLConfig is a simple wrapper to store name/value pairs in xml. You can access the full XML via cfg.Doc. > > - More generally, is XmlConfig suited to do more than a simple list You can store lists, trees, graphs as name/value pairs. > > (which would be even less than a .ini can do)? If you only use TXMLConfig and not the Doc then it can do the same as a TIniFile. Of course there are speed and size differences. > Now that I am thinking of it, I may be expecting too much. If XmlConfig > handles more than 2 levels, then why stop to 3? And then, how would > XmlConfig be different from a full Xml Implementation? TXMlConfig uses a pretty complete XML parser/writer.

Thanks to all the advice, I managed to make it work. I will add my example to the wiki in order to make this clear to other users as ignorant as I am :-)

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131127/8e361ab9/attachment-0001.html>