XML Process

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

XML Process

Post by Nakia » Thu Apr 04, 2013 5:58 am

Hi,

What is the normal process in dealing with loading preference files on Application startup.

I am building my first Desktop Applicatin and have come to the stage of needing to setup Preferences which will be stored in a file.
I am right now deciding if I should create this file as an XML File for portability into other systems or do I create my own format... opinions?

If I do decide to go with XML is it normal on application startup to create the XML Tree from the file and keep that in memory so all parts of the Application can use it if needed?
Is this standard practice...

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: XML Process

Post by Mark » Thu Apr 04, 2013 11:50 am

Hi,

This might get you started: http://qery.us/3ea

Feel free to ask questions on the blog.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: XML Process

Post by jacque » Thu Apr 04, 2013 7:02 pm

There isn't really a "normal" process, you can create your prefs file in whatever format you like. For simple apps, I've just used a text file with one preference per line, like this:

green
10
DaisyMae

My app knows which line means which preference. But lately I've been using stacks to store preferences because of the many storage options they provide. Usually I set a custom property to the preference, which gives me an automatic array and makes it easy to extract any preference I want:

set the cUserName of stack "prefs" to "DaisyMae"
set the cUserAge of stack "prefs" to "10"

put the cUserName of stack "prefs" into tUserName
--etc

I can store anything in custom properties and it is far easier than messing around with XML, which to me is a waste of time for something like this.

Different OSs require different places to store prefs files. You can use the specialFolderPath("preferences") or the application support folder on Macs -- specialFolderPath("asup"). There's a fantastic folder resource on Ken Ray's site, which I consult all the time: http://www.sonsothunder.com/devres/live ... ile010.htm Go back one page for a complete list of invaluable tricks and tips like this one.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: XML Process

Post by Simon » Thu Apr 04, 2013 7:17 pm

Hi jacque,
But lately I've been using stacks to store preferences
I'm getting the odd feeling that this says standalones can save themselves? Have I missed something maybe like standalones can't save themselves but it can create a substack that it can save? Maybe not even create but change an additional stack file that gets saved?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: XML Process

Post by jacque » Thu Apr 04, 2013 8:32 pm

No, standalones can't save themselves or save data to their substacks. The prefs stack I was talking about is a separate stack that is saved as an independent file in the appropriate preferences folder. Regular stacks are just documents and can be saved like any other app's documents.

My scripts typically create a new, blank stack that the user never sees, and save it to disk. This can happen on first launch, the first time you need to save a preference, or any other time:

Code: Select all

on savePref pPref,pValue
  put specialFolderPath("asup") & "/MyPrefs.lc" into tPrefsFile
  if there is no stack tPrefsFile then
   create invisible stack "myPrefs"
   set the filename of it to tPrefsFile
   save stack "myPrefs"
  end if
  set the pPref of stack "myPrefs" to pValue
end savePref
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: XML Process

Post by Simon » Thu Apr 04, 2013 8:55 pm

Hi jacque,
So the difference between saving a stack file and saving a text file is that you can use:

Code: Select all

put the pPref of stack "myPrefs" into myLocalVar
as opposed to:

Code: Select all

put url "file:myPrefs.txt" into myPrefs
put line 3 of myPrefs into myLocalVar
--or
put item 5 of myPrefs into myLocalVar
Allowing you to use a custom property rather than line/word/item to select?
I like that... "feels" more accurate.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: XML Process

Post by Nakia » Thu Apr 04, 2013 10:24 pm

Thanks for the advice.

I kind of agree with Jacque that the XML path seems unnecessarily complicated and I do like the method of saving the Prefs to a Stack File and this method
will overcome the scenario of looking for existing entries etc.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: XML Process

Post by Simon » Thu Apr 04, 2013 10:52 pm

Hi Nakia,
Mine is still a question up there which I'd like to add to.
Can the custom properties by used like objects?
eg
repeat for the number of customKeys of me --nope
or
repeat for each key tKey of the customKeys of me --nah that didn't work
put the number of customProperties of me into tKey --nope
hmmm... Without addressing the customProperty name specifically, (maybe too big a question) what other ways can one work with the whole data set?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: XML Process

Post by sturgis » Fri Apr 05, 2013 1:27 pm

You're close simon

the customkeys -- returns a line delimited list

so your first attempt could be written like so.

Code: Select all

put the customkeys into tKeys
repeat with i = 1 to the number of lines in tKeys
put line i of tKeys
end repeat
2nd method

Code: Select all

repeat for each line tLine in the customkeys of stack "timerclock"
put tLine
end repeat
also from your question change the next option to "put the number of lines in the customkeys of me.."

Might also look at custom property sets, and also be aware you can store an array in a custom property also. So you can have your entire prefs settings in an array and put it into a property, then retrieve the whole array intact from the property on the next run.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: XML Process

Post by Simon » Fri Apr 05, 2013 9:39 pm

Ha, funny me.
There I was extolling the virtues of using a custom property because it wasn't a chunk expression and the first thing I do is ask how to make it into chunks!
Ha

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply