New topic: Reading and Writing XML Files
<http://forums.realsoftware.com/viewtopic.php?t=47384> Page 1 of 1 [ 3 posts ] Previous topic | Next topic Author Message barrytraver Post subject: Reading and Writing XML FilesPosted: Sat Mar 23, 2013 4:49 pm Joined: Fri Sep 30, 2005 1:53 pm Posts: 868 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 ktekinay Post subject: Re: Reading and Writing XML FilesPosted: Sat Mar 23, 2013 9:17 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 532 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 barrytraver Post subject: Re: Reading and Writing XML FilesPosted: Sun Mar 24, 2013 2:33 am Joined: Fri Sep 30, 2005 1:53 pm Posts: 868 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 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 3 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
