views:

515

answers:

2

I got this problem, when edit my xml everything on a next line renders two breaklines

<![CDATA[Line one
Line two
Line three
]]>

Renders in flash as

Line One

Line Two

Line Three

Now I researched this before and it had something to do with hidden breakline characters, Im using Flexbuilder and or Aptana to edit the xml, but how do I avoid having two breaklines when I want one.

A: 

Perhaps you're inserting the wrong end-of-line characters. Usually your choices are: cr-lf, cr, and lf. The first works well on windows, the second on Mac, the third on most flavors of Unix. Try setting your editor to use lf (\n).

jeffamaphone
+1  A: 

Flash interprets both \r and \n as new line characters. If your input contains windows line endings (\r\n), then you see a blank line in the output.

To solve your problem, use the following regex to replace two consecutive occurrences of \r and \n with one before you output your data.

var newline:RegExp = /\r\n|\n\r/g;
var input:String = "your XML CDATA input";
var output:String = input.replace(newline, "\n");

Actually it is unlikely that \n\r ever occurs, but since we are already replacing, we can fix these invalid line endings as well.

Tomalak
makes sense, but isnt this a bit of an overkill work around ?
Kasper
It's the workaround that prevents the problem from ever surfacing again, independently of your editing habits/platforms and data sources. Wrap it in a function and you are done. Tweaking your editor works only as long as you don't forget to do it, tweaking external datasources may not work at all.
Tomalak
okay thanks, i applied it.
Kasper