Home About Contact

Reading HTML tags from XML into Flash

Posted by phorner On May 7, 2009

FlashOne problem that many people experience when working with XML and Flash is getting HTML tags inside the XML to be read in and rendered in Flash. Reading XML that contains HTML tags is a very common thing to do these days, as Flash gets used more and more to create applications since the release of Adobe AIR. So here is a tutorial showing you the basics of how to do it.

XML

The trick to getting the XML parser in Flash to ignore the HTML tags is to encapsulate the text string containing the HTML tags using CDATA. Below is an example:

<?xml version="1.0" encoding="iso-8859-1"?>
<section>
<content><![CDATA[Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><br>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.<br><br><font color="#0099FF">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</font><br><br>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br><br>List:<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>]]></content>
</section>

By encapsulating the text string inside <![CDATA[]]>, this tells Flash to just treat everything inside the CDATA tag as a pure string, and to ignore any HTML tags that it may contain.

Actionscript

Once this string is read into Flash and assigned to a variable (in my example, the variable name is htmlContent), you will then most likely need to display this text. Create a text field on the stage, and select the “Render text as HTML” button in the text field properties. Give the text field an instance name (e.g. “contents_txt”). Then assign the variable “htmlContent” to the “htmlText” property of the text field.

contentXML.onLoad = function() {
...
contents_txt.htmlText = htmlContent;
}

By assigning the string to “htmlText“, this tells Flash to then render the text inside the text field using the HTML tags. Simple!

I’ve created example files for you to download and look at. AS2 version is here and AS3 version is here.

Hope this helps some of you out there to get those HTML tags rendering in Flash.

Share

Comments are closed.