How to add a line break to a text coming from XML in Flash (AS2/AS3)?
I've been trying with \n and \r, and <br> using HTML text and it was breaking the XML; it could have been fixed with <![CDATA[ but I didn't like the solution, and in the end... I didn't want to use HTML text!

The answer was too easy, really:


&#xD;


This simple characters combination made it. Just put this inside your XML's texts to have a line break. Cool.
As many other times, I don't know why this is not advertised everywhere, it's the simplest and cleanest solution to add line breaks to a text field from XML.

Well, enjoy!

Now you can find a live example and download the sources.

UPDATE

A guy left a review on Stumble saying: "CDATA - Character Data. Use it. It's what it's there for".
At the very beginning I wrote that I didn't like this solution, but now I'd like to give more information on why I DON'T like CDATA:

- It increases the file size
- Your XML file becomes bloated and less readable
- If you need a new line break (which is the point of this post) you have to actually make the new line in the XML, destroying the formatting

Here is an example:
<items>
<item><![CDATA[This is the first line.
And this goes on a new line.]]></item>
</items>

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
  • StumbleUpon
  • Google Bookmarks
  • Technorati
  • Netvibes
  • LinkedIn
  • Live
  • Reddit
  • RSS