Copy link to clipboard
Copied
I'm loading text from an xml file that has some CDATA as well as <span class> in it. The xml file would look like this:
<?xml version="1.0"? encoding="utf-8"?>
<allPages id="Execution Management - Replanner" version="1.6" template="djc2_1_5" audioType="MP3" videoType="WMV">
<page id="8">
<narration>
<![CDATA[hello<step>Step 1 text <span class = 'mycolor'>with some text followed by a comma</span>, followed by some more text</step>]]>
</narration>
</page>
</allPages>
The problem I'm getting is when i put the CDATA text into a textfield, an extra space is being added after the </span. ie. "with some text followed by a comma ,". This space is being added only if there is some kind of punctuation immediately following </span> in the xml document. I've tried getting rid of it with regular expressions and split().join but nothing has worked.
that makes no difference (except to show that whoever created that xml format using <step> in the cdata instead of step tags in the xml wasn't knowledgeable).
it also points out that your problem has nothing to do with </span> or the css. the problem is whatever you're doing to handle the <step> tag is introducing the problem.
anyway, just use:
function f(e:Event):void{
var xml:XML = XML(urlloader.data);
var s:String = xml.page.narration.text();
var a:Array = s.split('</step>').join('').split('<ste
...Copy link to clipboard
Copied
what is <step>?
and, more importantly, what are the mycolor attributes that are causing that problem?
Copy link to clipboard
Copied
Ultimately, the xml document will have alot of pages (page id="1", "2" etc.) and within the CDATA, there will be several steps. I just simplified everything in the example. In Flash, I'm accessing "page 8" and putting the text between <step></step> into an array (stepArray) and when a button is clicked, a textfield gets populated with stepArray[0]. Here, I only have one "step" but there will be a stepArray[1], stepArray[2] etc. CSS is applied to the textfield so mycolor is setting that specific text to red.
I'm stuck with how the xml is formed since it's generated from some custom application. The function in flash doing all of this is
function showXML(e:Event):void
{
XML.ignoreWhitespace = true;
var xml:XML = new XML(e.target.data);
var stepString = "<allSteps>"+xml.page[8].narration+"</allSteps>";
var output:XML = new XML(stepString);
for each ( var step:XML in output.step){
stepArray.push(step);
}
}
stepTxt.condenseWhite=true;
Copy link to clipboard
Copied
again, what are the mycolor attributes that are causing that problem? ie, copy and paste that class.
Copy link to clipboard
Copied
var css:StyleSheet = new StyleSheet();
css.parseCSS('.mycolor { color: #CC0000; font-weight: bold }');
I don't know what's causing the extra space. The text that is displayed in the textfield is "Step 1 text with some text followed by a comma , followed by some more text". The stylesheet colors the appropriate text red but it shouldn't be "comma ," It needs to be Step 1 text with some text followed by a comma, followed by some more text
Copy link to clipboard
Copied
that css isn't quite right but i don't see a problem even using it. there should be a semi-colon after bold or you won't see a bold font in mycolor.
create a new fla, copy and paste the xml you showed and save it as test.xml and add to the actions panel and test. any problem?
var tf:TextField = new TextField();
tf.border = true;
tf.multiline = true;
tf.wordWrap = true;
tf.width = 400;
addChild(tf);
var css:StyleSheet = new StyleSheet();
css.parseCSS('.mycolor { color: #CC0000; font-weight: bold; }');
tf.styleSheet = css;
var urlloader:URLLoader = new URLLoader();
urlloader.addEventListener(Event.COMPLETE,f);
urlloader.load(new URLRequest("test.xml"));
function f(e:Event):void{
var xml:XML = XML(urlloader.data);
tf.htmlText = xml.page.narration.text();
}
Copy link to clipboard
Copied
This might be getting closer, but the xml will utimately look more like this
<?xml version="1.0"? encoding="utf-8"?>
<allPages id="Execution Management - Replanner" version="1.6" template="djc2_1_5" audioType="MP3" videoType="WMV">
<page id="8">
<narration>
<![CDATA[<step>Step 1 text <span class = 'mycolor'>with some text followed by a comma</span>, followed by some more text</step><step>Step 2 text<span class = 'mycolor'>with some text followed by a comma</span>, followed by some more text</step><step>Step 3 text<span class = 'mycolor'>with some text followed by a comma</span>, followed by some more text</step>]]>
</narration>
</page>
</allPages>
I'd have 3 buttons and when button1 is clicked, the step 1 text would show in the textfield. button2 would show the step 2 text etc. without any extra spaces, new lines, line breaks etc. showing up. With the original code I was able to push each step into an array. When condenseWhite is set to false, I can kinda see what's happening. The text then looks like
Step 1 text
with some text followed by a comma
, followed by some more text
So mycolor is put on a new line by itself and condenseWhite=true puts it all together but with a space after the word comma(or whatever the last word happens to be). Maybe there isn't a way around this or if there is, it's with some kind of regular expression.
Copy link to clipboard
Copied
was there any problem using the code i suggested?
Copy link to clipboard
Copied
That example works but i won't be able to just say tf.htmlText = xml.page.narration.text(); The example has only one <step></step> so that works. I'll need to so something like when a button is clicked, tf.htmlText = xml.page.narration.step[0];, tf.htmlText = xml.page.narration.step[1]; etc. since there's going to be many many steps within the CDATA and they'll need to show individually in the text box.
Copy link to clipboard
Copied
that makes no difference (except to show that whoever created that xml format using <step> in the cdata instead of step tags in the xml wasn't knowledgeable).
it also points out that your problem has nothing to do with </span> or the css. the problem is whatever you're doing to handle the <step> tag is introducing the problem.
anyway, just use:
function f(e:Event):void{
var xml:XML = XML(urlloader.data);
var s:String = xml.page.narration.text();
var a:Array = s.split('</step>').join('').split('<step>');
a.shift();
}
Copy link to clipboard
Copied
Everything is working now. Thanks.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now