Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Jun 24, 2018 4:11 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Reading and Writing XML Files
PostPosted: Sat Mar 23, 2013 4:49 pm 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
My earlier methods of writing code to read and write XML files has become "deprecated." The following is my attempt to write replacement code to read and write XML files:

Dim XMLType As New FileType
XMLType.Name = "XML files"
XMLType.Extensions = ".xml"
Dim FileStreamA as TextInputStream
Dim FileStreamB As TextOutputStream
Dim File1 As FolderItem
Dim MainBuffer As String
Dim XMLLines() As String

// Read the file:
File1 = GetOpenFolderItem( XMLType )
If File1 <> Nil Then
FileStreamA = TextInputStream.Open ( File1 )
MainBuffer = FileStreamA.ReadAll
FileStreamA.Close
End if

XMLLines = Split ( MainBuffer, EndOfLine )
// modify some XML lines as desired
MainBuffer = Join (XMLLines)

// Write the file:
File1 = GetSaveFolderItem(XMLType, File1.Name )
If File1 <> Nil Then
FileStreamB = TextOutputStream.Create ( File1)
FileStreamB.Write MainBuffer
FileStreamB.Close
End if
0
Is the preceding code acceptable? Or are there changes that should be made?

Also, I'm accustomed to see an instance followed by a period followed by a method (or property or event). An example is FIleStream.Close. But TextOutputStream.Create doesn't fit that pattern. Yes, there's a period and a Method (.Create), but TextOutputStream is not an concrete instance but a more abstract model.

To put it a different way, when a programmer writes Dim A As B, the A is an instantiation of the general pattern B. Isn't that how instantiation works? Thus FileStreamB is an instantiation of TextOutputStream, not the other way around.

I'm obviously misunderstanding something here. What is it?

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: Reading and Writing XML Files
PostPosted: Sat Mar 23, 2013 9:17 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
First, your Join command is missing EndOfLine.

Second, I wonder why you're not using the XMLDocument class to manipulate XML? You assumption is that the XML document you're parsing is split into paragraphs, but it doesn't have to be. Maybe it's a valid assumption, I don't know, but still, XMLDocument is there for you.

To answer your question, a class can have a Method (called from the instance of a class) or a Shared Method (called from either the instance or the class itself), and "TextInputStream.Create", et al, is the latter.

A Shared Method makes the class work like a module and knows nothing about the instance you may be calling it from. Hence, "me" or "self" are meaningless in a Shared Method, nor can it access any properties specific to an instance of the class.

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Reading and Writing XML Files
PostPosted: Sun Mar 24, 2013 2:33 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
ktekinay wrote:
First, your Join command is missing EndOfLine.

Kem,

Thank you, thank you, thank you. I was making an incorrect assumption about REALbasic's Join that was causing me all kinds of problems. Instead of using REALbasic's Join, I had been using a "MyJoin" that someone had written for me years ago (back when REALbasic's Join had not been perfected). That MyJoin worked fine, but I was attempting to bring my code up to date by switching over from MyJoin to Join and wrongly assumed that the two behaved the same. They didn't and they don't.

My experience shows how dangerous it is to "go with the defaults" and to make assumptions without checking out the premises. You asked why my "Join command is missing EndOfLine." Technically speaking, it wasn't "missing" anything, because the delimiter for REALbasic's Join is optional. As the REALbasic Language Reference indicates, that Join has an "Optional delimiter used in separating the elements of sourceArray when creating result."

The problem for me is/was that "The default is one space." And I have no excuse for missing that fact, because the Language Reference repeats it: "If no delimiter is passed, a single space will be used as the delimiter."

To confuse things for Windows users, Microsoft uses a different EndOfLine delimiter inside a TextArea control (or its equivalent) than it uses outside such a control in a normal string. Mac and Linux users don't have that problem. I mistakenly thought that I'd avoid the trouble by letting REALbasic supply the default delimiter (BIG MISTAKE on my part).

In one short sentence (the "First" thing you wrote, in fact), you solved a problem that I was having no progress in solving. Thanks again; I owe you one.

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: Reading and Writing XML Files
PostPosted: Sun Mar 24, 2013 4:17 pm 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
My question involves the following code:

MainBuffer = Join ( XMLLines, EndOfLine )
XMLLines = Split ( MainBuffer, EndOfLine )

My (incorrect?) assumption would normally be that -- as long as you don't have anything elsewhere in your Project to change the values of MainBuffer (a string) and XMLLines ( ) (a string array) -- both MainBuffer and XMLLines ( ) will never change but always have the same value. (MainBuffer and XMLLines ( ) are defined as global properties.) But they DO seem to change, and I can't figure out why or how.

I should (I would think) be able to insert those two lines (kept together) anywhere (and as often as I want) in my code and have it still work:

MainBuffer = Join ( XMLLines, EndOfLine )
XMLLines = Split ( MainBuffer, EndOfLine )

MainBuffer = Join ( XMLLines, EndOfLine )
XMLLines = Split ( MainBuffer, EndOfLine )

MainBuffer = Join ( XMLLines, EndOfLine )
XMLLines = Split ( MainBuffer, EndOfLine )

MainBuffer = Join ( XMLLines, EndOfLine )
XMLLines = Split ( MainBuffer, EndOfLine )

Can MainBuffer and XYMLines ( ) have different values within two different Methods?? It makes no sense.

Now, the problem seems to occur only when my accessing of MainBuffer or XMLLines is spread over two different Methods, but I seem to have a problem even if the different Methods are in the same portion of code (e.g., Window1).

I suspect that the problem may be related to the string array (I haven't been able to track down any information on this, but I vaguely remember something about some things not being able to be done in a regular Window). My searches (including Google) have led to no solution.

Does that sound familiar to anyone? Or do you have some other ideas as to what may be going on here?

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: Reading and Writing XML Files
PostPosted: Sun Mar 24, 2013 5:12 pm 
Offline

Joined: Fri Dec 01, 2006 3:09 pm
Posts: 650
Location: Tennessee, USA
Barry, you need to elaborate on what the change is. You say that things change but you don't say how or what so it is tough to begin to attempt an answer.

Just looking at the code where the calls are repeated multiple I can see where a potential problem could lie. That would be if the XMLLines had lines that had EndOfLine in them to start with. You could start with say 100 elements in the array and if five of those had a EndOfLine character in them the the Split following the Join would produce 105 elements. But the number of lines should stay at 105 in the following groups of calls in that case.

Give us more info on what this change is that you are seeing.


Top
 Profile  
Reply with quote  
 Post subject: Re: Reading and Writing XML Files
PostPosted: Sun Mar 24, 2013 5:36 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
IF nothing is changing either MainBuffer or XMLLines(), and IF all your code is accessing the same properties (ie., you don't have either one defined more than once), then your assumption is correct. Split and Join would not change the result. Even if some of the lines had endofline characters, the final joined string would still be intact.

What you describe is consistent with having one or both properties defined in more than one location and each block of code using whichever version of the property is closest in scope.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group