How to write in data to an XML file

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

How to write in data to an XML file

Post by Mag » Sat Dec 29, 2012 6:20 pm

Hi all!

Being implementing preferences in my app, I followed this great tutorial that explains how to read in data from an XML file http://lessons.runrev.com/s/lessons/m/4 ... n-xml-file. Now I would like to write in data, unfortunatly I can't find info on this. Any help would be greatly appreciated.

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

Re: How to write in data to an XML file

Post by Simon » Sat Dec 29, 2012 8:12 pm

Hi Mag,
Look up revAppendXML in the dictionary.
If you just type "xml" in the dictionary you'll see all the xml commands, functions and messages.

Simon

EDIT: Just wanted to check, do you need to use xml? A simple text file can be used for saving preferences and it's a lot simpler then dealing with a bunch of nodes.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: How to write in data to an XML file

Post by Mag » Sat Dec 29, 2012 9:51 pm

Hi Simon, thank you for the info, I will check the dictionary. I have just some booleans and string values to store, do you suggest I could use a simple text file?

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

Re: How to write in data to an XML file

Post by Simon » Sat Dec 29, 2012 10:59 pm

Yes, Yes, Yes,
Why make life tough, if the data is only going to be used by your app then keep it simple.
Lets say it's a solitaire game and you want to store some values like:
Name,DeckStyle,CardStyle,HighScore1,HighScore2,HighScore3
Then set up a var to hold those settings:

Code: Select all

local tFileContents
on preOpenCard
   put url ("file:" & specialFolderPath("documents") & slash & "data.txt") into tFileContents
   if the result <> empty then
      --no file found start a new one
      put comma & comma & comma & comma & comma & comma into tFileContents
      put tFileContents into url ("file:" & specialFolderPath("documents") & slash & "data.txt") --save the file
   end if
   --fill the UI
   put item 1 of tFileContents into field "Name"
   set the DeckStyle to item 2 of tFileContents
   set the CardStyle to item 3 of tFileContents
   put item 4 of tFileContents into field "HighScore1"
   put item 5 of tFileContents into field "HighScore2"
   put item 6 of tFileContents into field "HighScore3"
end preOpenCard
As values change:

Code: Select all

put tNewHighScore into item 4 of tFileContents
put tFileContents into url ("file:" & specialFolderPath("documents") & slash & "data.txt")
You will have a text file that looks like:
Mag,Blue,Red,500,457,350

gosh, that kind of looks complicated but it's not.... really.
Now if this app is to be used with double byte characters (like Chinese characters) then there is a bit more to do but look up "htmlText".

Simon
EDIT: (I always do these things to quickly) Those "Set the.." don't really work, but I hope you get the idea. :)
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: How to write in data to an XML file

Post by Mag » Sun Dec 30, 2012 2:44 pm

Simon, thank you for your advices. I will go with the .text option then. Interesting the non-latine chars thing, actually I have a field where users could enter non-latine chars and we have to keep in mind also that in iOS people uses emoticons... so maybe it's better to support this from the start.

I found htmlText in dictionary and it says:
Summary: Specifies the contents of a field, with its text formatting represented as HTML tags and special characters represented as HTML entities.

So now I'm studying as this could impact in my text file...

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

Re: How to write in data to an XML file

Post by Simon » Sun Dec 30, 2012 8:32 pm

Hi Mag,
Yes, that Name field in the example reminded me that they could use double byte.
Also look up Unicode. That is the "standard" method of encoding/decoding.
Now emoticons are another thing if you are looking to show images like this :lol: as they are .gif. <img src="./images/smilies/icon_smile.gif" alt=":)" title="Smile" /> That would be a whole other subroutine not even part of the double byte stuff.
You will also want to change "...into url ("file:"..." to into url ("binfile:" as file saves as a text file and binfile saves as a binary file.

All of this would be required even if it were an xml file.

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

Post Reply