Loading Text From XML
Made a silly mistake while loading text from an external XML file and I really really don’t want anybody to follow in my footsteps.
<strings> <string id="someString"> <![CDATA[Sentence one\nSentence two]]> </string> </strings>
Does the above code looks flawed to you? Take a moment and look it through.
When you do the below AS2 codes in flash, what the \n does in the string is basically creating a newline, and thus "Sentence two" will appear below "Sentence one".
var txt:TextField = createTextField( "txt", _root.getNextHighestDepth(), 100, 100, 200, 200 ); txt.text = "Sentence one\nSentence two";
Happily, I ported all these original strings into an XML and got it loaded. What puzzled me was that, when these text were actually loaded, the \n didn’t work! The textfield simply displayed "Sentence one\nSentence two" without the newline. I sought help from a fellow colleague, Arul, and together, we still could not see what’s wrong with it. We left office without solving that "bug".
That night, Arul called to say, "Hey, with CDATA, we don’t need the \n! We simply just need to hit <enter>!" Immediately it struck me, oh ya! That’s the whole idea of using CDATA isn’t it? We got too engrossed with the wrong problem!
The next day, I changed all the \n to <enter> key, and expected things to work. Unfortunately though, the text did not turn out as expected. Flash had interpreted my <enter> in XML as double <enter>s when it’s being displayed on the textfield!
I sought Arul’s advice again and this is what I got. Flash interprets <enter> in external text sources as "\r\n", which explains the double <enter>s being rendered. So, what I actually had to do is to run through the string, strips off the "\r", and change it to "" (empty string).
Here’s the code that solves the above issue.
function processString ( s:String ):String { return s.split("\r").join(""); }
Hope this helps!







Yea, that was a classic example of ‘trying to solve the wrong problem’ case.
>>’Flash interprets in external text sources as "\r\n" ‘
You got me wrong there. Its not Flash which interprets it differently. May be I should’ve clarified in detail.
Internally, Flash always represents newlines as "\r". Most of us usually wrote "\n" when we wrote AS code, but since "\r" and "\n" are handle like synonyms in the flash player, we scarcely ever find a problem.
But each OS has a different way of handling newLine characters; Unix flavors ( and Mac OSX) represent newline as "\n". Where as MacOS classic (9 and below) handled newLine as "\r". When it comes to Windows, its represented as "\r\n".
Now, since the XML you are trying to load is on a Windows machine, those enters that you hit were saved as "\r\n".
And then when it comes inside a Flash player ( which takes \r and \n as two different entities, it shows them as two lines.
So finding "\r" and removing them off - or - finding "\r\n" and replacing with "\n" (or
\r") will mean that we help the flash player to treat the textfile the same way, in windows and mac and unix.
You get it now?
haha
>> Most of us usually wrote "\n" when we wrote AS code
No idea why I typed "wrote" when I was thinking "write". Time to sleep, may be?
w00t!
It’s the <enter> and <carriage return> difference that you’re referring!
Actually I knew about this in which different OS treating <enter> keys differently, but the thing I wasn’t clear about was "\n" and "\r".
Before you explained in the above, to me, "\n" was like a newline kind of "command" to me, which I did not relate at all to <enter>.
Can I assume the below is correct then?
\r ==<carriage return>
\n ==<enter>
Thank you Thank You Thank You!
Two days of banging my head again the wall until I found this.
Thank You Thank You Thank You!