Page 1 of 1

How to write in data to an XML file

Posted: Sat Dec 29, 2012 6:20 pm
by Mag
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.

Re: How to write in data to an XML file

Posted: Sat Dec 29, 2012 8:12 pm
by Simon
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.

Re: How to write in data to an XML file

Posted: Sat Dec 29, 2012 9:51 pm
by Mag
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?

Re: How to write in data to an XML file

Posted: Sat Dec 29, 2012 10:59 pm
by Simon
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. :)

Re: How to write in data to an XML file

Posted: Sun Dec 30, 2012 2:44 pm
by Mag
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...

Re: How to write in data to an XML file

Posted: Sun Dec 30, 2012 8:32 pm
by Simon
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