This article is to illustrate how to use HTML text inside an XML file so that Flash can load the XML and render the text as HTML.
The introduction of HTML strings in an XML file is done via the XML CDATA tag.
The Flash"s parser does not interpret the XML tag Character Data (CDATA). This way, we can retrieve with precision the HTML text format.
Let"s see how"
I create a FLA and save it as "xml_e_html.fla".
I create a dynamic multiline text field and I call it "test_txt".
Now, I create 2 single line text fields to insert the font in the SWF and as we will need Bold and Italic, the text fields will be 2.
See article "
Embed fonts in a SWF
This way, I will have 3 text fields as follow:
- the main "test_txt" (multiline) to which I will insert the sets of character I prefer
- a dynamic text field (single line) with the option Bold turned on to which I will insert the same sets of characters as "test_txt".
- a dynamic text field (single line) with the option Italic turned on to which I will insert the same sets of characters as "test_txt".
As from now, I am sure that all the fonts needed will be included in the SWF when published.
I drag on stage a scrollBar component from the components window, open the parameters window of the component and in the option ScrollTargetName , I insert the instance name of the multiline text field (test_txt).
I now create a Document Class, an AS file saved as "Main.as", which will load the XML file as follow:
Code:
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.*;
public class Main extends MovieClip
{
public function Main()
{
this.loadXML();
}
private function loadXML():void
{
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeHandler);
var request:URLRequest=new URLRequest('testo.xml');
try
{
loader.load(request);
}
catch(error:Error)
{
trace('Unable to load requested document.');
}
}
private function completeHandler(event:Event):void
{
var loader:URLLoader=URLLoader(event.target);
var result:XML=new XML(loader.data);
var myXML:XMLDocument=new XMLDocument();
myXML.ignoreWhite=true;
myXML.parseXML(result.toXMLString());
var node:XMLNode=myXML.firstChild;
test_txt.htmlText=node.firstChild.firstChild.nodeValue;
}
}
}
Here is the result:
Click here to see the XML file.
See you next"