Skip to main content
Inspiring
May 21, 2013
Question

doesn't read XML line breaks

  • May 21, 2013
  • 1 reply
  • 1896 views

Here's a portion of my XML file:

<?xml version="1.0" encoding="utf-8"?>

<personagequotes>

<personage>

<fragment>

<quote>This is a text.\n This line should be the second but isn't</quote>

<film>001.flv</film>

</fragment>

...

I'm using this code to read the XML and put the content of node 0 in a textfield:

var personageQuotes_XML:XML;

var xmlLoader:URLLoader = new URLLoader();

xmlLoader.load(new URLRequest("quotes.xml?rand="+Math.random()*1000));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

//;

function errorHandler(event:IOErrorEvent):void

{

          //error

}

//

function xmlLoaded(event:Event):void

{

          personageQuotes_XML = new XML(xmlLoader.data);

          var quote:String = personageQuotes_XML.personage[0].fragment.quote[0];

          myText.wordWrap = true;

          myText.multiline = true;

          myText.text = quote;

}

Problem is that it doesn't read the line break. Instead it puts this in the textfield:

'This is a text.\n This line should be the second but isn't'

I've also tried with <br> </br> and <br/>, but then I only got errors about a quote tag not being properly closed etc.

How to I make line breaks / carriage returns work when reading a XML file?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
May 21, 2013

See if using  &#10;  works. 

jiggy1965Author
Inspiring
May 21, 2013

No, it was actually a problem with the XML Class I discovered :

When you read an XML file that has newlines, Flash automatically strips them out. This fixes that. It's useful for displaying plain text in textfields.

function fixXMLNewlines(str:String):String{

var new_str:String = str.split("\\n").join("\n")return new_str

}

Seems that Flash reads an XML file and doesn't read backslashes as escape characters. So when it reads 'line1\rline2' it actually makes 'line1\\rline2' of it, a string which traces as 'line1\rline2' again. Above code fixes that.

Don't know why the XML Class does that?

Ned Murphy
Legend
May 21, 2013

Using the &#10; is a way around that, though I don't know if it applies in AS3 - haven't had occasion to it myself. 

The processing of a "\n" line feed is bypassed when you place it in an xml file and read it in.  It gets reprocessed as \\n which is why doing the relacment you show takes care of it