Skip to main content
Participating Frequently
February 24, 2010
Question

Preserving whitespace in html to TextConverter

  • February 24, 2010
  • 1 reply
  • 783 views

The XML objects in Flash have a tendency to remove all whitespace. So if I choose to represent HTML as XML when I'm passing it to TextConverter or an importer:

<html>My <b>great</b> idea</html>

- it trims the space after "My" and before "idea", so my text looks like this:

Mygreatidea

- which is unacceptable.

Right now I'm doing this weird jump-through-hoops in order for Flash to preserve that white space:

XML.ignoreWhitespace = XML.prettyPrinting = false; myXML = <html>My <b>great</b> idea</html>; XML.ignoreWhitespace = XML.prettyPrinting = true;

- which works, but is awful to look at. Is there any other way to preserve whitespace in the HTML? (And passing it as a String instead is not an option, because then all the quote characters have to be escaped, which makes things even worse.)

This topic has been closed for replies.

1 reply

Adobe Employee
February 24, 2010

There's a setting in Flash's XML object called ignoreWhitespace that you need to set to false (by default its true).

Something like this should work:

function convertStringToXML(source:String):XML

{

     var originalSettings:Object = XML.settings();

     try

     {

          XML.ignoreWhitespace = false;

          var xmlTree:XML = new XML(source);

     }

     finally

     {

          XML.setSettings(originalSettings);

     }

     return xmlTree;

}

- robin

RezmasonAuthor
Participating Frequently
February 24, 2010

Well, as you can see I'm doing that. I'm setting it (and prettyPrinting, it seems to me that I need to toggle both) to false, passing an XML string, and then setting it to true again. Unfortunately, I use XML in other parts of my app that probably should be trimming this whitespace.

If this is the only solution, then I'm fine with it. It's just a real bother.

Adobe Employee
February 24, 2010

I don't know of a better way. There is no way for Flash to know which way you want it set without your telling it, and there's no place in the actual conversion call to pass the information. You could isolate the conversion in a function that has the ignoreWhitespace in a parameter, that might be a little cleaner. Then you're always sure that you didn't leave it in the wrong state, or didn't consider how it should be set when doing the conversion.