Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Several ways to approach this. Perhaps the simplest answer is, in one sense your Flash developer is probably right, and you should move your whitespace outside of the CDATA container. The reason being, many people (me at least) tend to assume that everything inside a CDATA is "real data", as opposed to markup. On the other hand, whitespace <em>outside</em> a CDATA is normally assumed to be irrelevant, so data like this:</p> <pre><code>&lt;description&gt; &lt;![CDATA[&lt;h2&gt;lorem ipsum&lt;/h2&gt; &lt;p&gt;some text&lt;/p&gt;]]&gt; &lt;/description&gt; </code></pre> <p>would be easier to understand and to work with. (The flash developer can use the <code>XML.ignoreWhite</code> property to ignore the whitespace outside the CDATA.)</p> <p>With that said, if you're editing the XML by hand, then I can see why it would be easier to use the formatting you describe. However, if the extra whitespace is inside the CDATA, then it will inevitable be included in the String data you extract, so your only option is to grab the content of the CDATA and remove the whitespace afterwards. </p> <p>Then your question reduces to "how do I strip leading/trailing whitespace from a String in AS2?". And unfortunately, since AS2 doesn't support RegEx there's no simple way to do this. I think your best option would be to parse through from the beginning and end to find the first/last non-white character. Something along these lines (untested pseudocode):</p> <pre><code>myString = stuffFromXML; whitespace = " " + "\t" + "\n" + "\r" + newline; start = 0; end = myString.length; while ( testString( myString.substr(start,1), whitespace ) ) { start++; } while ( testString( myString.substr(end-1,1), whitespace ) ) { end--; } trimmedString = myString.substring( start, end ); function testString( needle, haystack ) { return ( haystack.indexOf( needle ) &gt; -1 ); } </code></pre> <p>Hope that helps!</p> <p>Edit: I notice that in your example you'd also need to remove tabs and whitespace <em>within</em> your text data. This would be tricky, unless you can guarantee that your data will never include "real" tabs in addition to the ones for formatting. No matter what you do with the CDATA tags, it would probably be wiser not to insert extraneous formatting inside your real content and then remove it programmatically afterward. That's just making your own life difficult.</p> <p>Second edit: As for what character to remove to get rid of newlines, it depends partially on what characters are actually in the XML to begin with (which probably depends on what OS is running where the file is generated), and partially on what character the client machine (that's showing the flash) considers a newline. Lots of gory details <a href="http://en.wikipedia.org/wiki/Newline" rel="nofollow noreferrer">here</a>. In practice though, if you remove <code>\r</code>, <code>\n</code>, and <code>\r\n</code>, that usually does the trick. That's why I added both \r and \n to the "whitespace" string in my example code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload