<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Interactive IQ : The Hessler Design Blog &#187; XML</title>
	<atom:link href="http://www.hesslerdesign.com/blog/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hesslerdesign.com/blog</link>
	<description>Smart Developers = Smart Websites</description>
	<lastBuildDate>Thu, 26 Jan 2012 23:00:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Top Ten Ways to Optimize Your Flash Website</title>
		<link>http://www.hesslerdesign.com/blog/flash/top-ten-ways-to-optimize-your-flash-website/</link>
		<comments>http://www.hesslerdesign.com/blog/flash/top-ten-ways-to-optimize-your-flash-website/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 17:45:12 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=324</guid>
		<description><![CDATA[Let&#8217;s face it.  When it comes to Flash websites, people either love them or hate them.  For those who hate them, I firmly believe it&#8217;s because of two main reasons: crappy SEO (search engine optimization), and long load times (a direct result of heavy file weights).  While the issue of SEO is its own beast, [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s face it.  When it comes to Flash websites, people either love them or hate them.  For those who hate them, I firmly believe it&#8217;s because of two main reasons: crappy SEO (search engine optimization), and long load times (a direct result of heavy file weights).  While the issue of SEO is its own beast, I won&#8217;t get into that now.  For now, I&#8217;m going to focus on 10 easy ways to optimize your Flash website to make it meaner, leaner, and more user-friendly.  Believe me, if you start using some of these ideas, your sites will transform in ways you never though imaginable.  And you&#8217;ll notice that you&#8217;ll get a much smoother and quicker site.  With that said, let&#8217;s get started.</p>
<h4>1. Use XML files to set content framework</h4>
<p>On nearly every Flash site I work on, I use at least some XML. The great thing about XML files is they are a great way to structure blocks of your site.  For instance, if you want a slideshow on your site, you could have an XML file that contains the title, url, and other image-specific information you want to include for each image. Then, just import the XML into Flash and store the XML data in arrays to organize the information to use later.  (More on arrays below.)</p>
<p>Another great thing about using XML in Flash is that if you want to stream an RSS feed into a Flash site, you can, because RSS feeds are set up as XML.  (For more info on RSS feeds, see the post on <a href="http://www.hesslerdesign.com/blog/flash-actionscript/streaming-rss-feeds-into-your-flash-application/" target="_self">Streaming RSS feeds into Your Flash Applications</a>.)</p>
<p>Additionally, XML is easy to change without having to republish your site.  Say you have that slideshow I mentioned above, but want to swap out an image.  With XML, you just point to the new url, update the title, and save. Your Flash site will automatically update with the new info, and you won&#8217;t have to republish every time. Trust me, you&#8217;ll thank yourself for using XML when you build your Flash site.</p>
<h4>2. Use arrays to organize assets used in your code</h4>
<p>For me, using arrays is quite possibly one of the smartest and most powerful things you can do to optimize your code.  Not necessarily from a file weight perspective, but because with arrays, you can reference an object stored in an array by name, number, id, etc. For instance, say you have 20 thumbnail images you want to make clickable to open a larger image. You could attach individual event listeners to each image.  Or, you could set up a couple of arrays &#8212; one for the instance names of each image, and one for the reference to the larger image to load when clicked &#8212; and run &#8220;for&#8221; loops to set the event listeners all at once. While you&#8217;re in the &#8220;for&#8221; loop, set an ID number for each image that you can then reference in your &#8220;click&#8221; function that will fire on mouse click, and call up that ID number slot in the large image array.  With only a few lines of code, you&#8217;ll have a powerful way to batch chunks of your code with minimal effort. (For more info on arrays, see my post on <a href="http://www.hesslerdesign.com/blog/flash-actionscript/utilizing-the-power-of-arrays-in-adobe-flash/" target="_self">Utilizing the Power of Arrays in Adobe Flash</a>.)</p>
<h4>3. Only import and embed absolutely necessary image files into your file.</h4>
<p>If you&#8217;ve been on the Web at all&#8230;and judging by you reading this, I&#8217;m guessing you have&#8230;you know doubt know that a lot of people don&#8217;t like Flash sites.  The main reason seems to be they take way too long to load.  To get around this, I highly suggest importing and embedding ONLY the images you absolutely need.  If you have a lot of image assets, I suggest loading them dynamically via code, and placing them in the appropriate places.  This not only makes the file size considerably smaller, but it also opens the door to be able to update an image, replace it, and have Flash automatically read in the updated file without the need to republish the entire site.  (Sorta sounds like one of the key perks of using XML, doesn&#8217;t it?)  And trust me, users will thank you for having a site that loads (or at least shows up with a short preloader) in 5 seconds, rather than a beefy site that doesn&#8217;t show up until the entire thing loads.</p>
<h4>4. Load assets in an orderly fashion, not all at once</h4>
<p>Along with #3 above, when you load assets into your Flash site dynamically, it&#8217;s best to set up a structure and order in which to load them. For instance, load all of the home/landing page assets first. Once those are loaded, proceed with the next set of assets that you think will be needed/viewed by your users.  Not using a certain set of images until later in the site?  Don&#8217;t load them up front. Wait until the rest of the assets before it have been loaded first. Think about it.  Would you rather try to have 20 images load all at once?  Or load them in the order of which they&#8217;ll be seen? Loading them one at a time will speed up the individual image load time because there is only one image needing attention while it is being loaded, not all 20?</p>
<h4>5. Cool it with the videos</h4>
<p>Okay, this sounds harsh, but hear me out. Video is great. It&#8217;s everywhere, and it&#8217;s getting more popular by the day.  (YouTube, anyone?)  But when you&#8217;re making a Flash site, video can also be a beast to work with, specifically video that needs to stream without a playback bar.  The worst thing about video is file size. I&#8217;ll admit, video has come a long way in optimizing file weights and all, but at the end of the day, even a small video is still going to be around 1-2MB.  When you are using video, make sure you utilize the &#8220;loading assets in an orderly fashion&#8221; advice from above.  If you try to start streaming a video when 20 images are loading while it&#8217;s trying to play, you&#8217;ll no doubt get choppy playback.</p>
<h4>6. Use TweenMax or TweenLite for code-based tweening and animating</h4>
<p>I&#8217;m a big fan of keeping my Flash sites on one frame of the timeline, and doing as much as I possibly can with code, rather than with the timeline.  The reason for this is simply less time making changes later on down the road.  Want to make every tween of an image fading out half as long across the entire site?  Pretty laborious if you&#8217;re using the timeline.  But if you&#8217;re using code, it&#8217;s a matter of updating a couple of numbers.</p>
<p>This is where <a href="http://blog.greensock.com/tweenmax/" target="_blank">TweenMax</a> and <a href="http://blog.greensock.com/tweenlite/" target="_blank">TweenLite</a> come in.  In a nutshell, they&#8217;re the best tweening engine out there.  Using it, you can call one function that can alter many facets of an object at once (x and y position, alpha, tint, etc.).  You can also attach event listeners like onComplete, onStart, etc., that call functions to run at specific times during the tween.  In short, these tweening engines will make you wonder why you ever used timeline-based animations.  And best of all, they&#8217;re extremely light in file weight, unlike a lot of timeline-based animations.  And it&#8217;s a one-time file weight addition, not a per-tween sort of thing.</p>
<h4>7. Simulate downloading on slower connections</h4>
<p>Not everyone has a cable-speed connection. Believe it or not, there are still a lot of people out there using DSL or slower connections.  And as unfortunate as it may be, those are the people you need to build the site for.  Don&#8217;t ever assume that your audience is going to have the latest and greatest computers.</p>
<p>To simulate downloads in Flash, export your movie and select &#8220;View &gt; Simulate Download&#8221;.  To set different connection speed settings, select &#8220;View &gt; Download Settings&#8221;, and select your desired speed (or create your own custom speed).  Flash can be deceiving. When you test out a movie, it always runs lightning fast. But by testing it using the &#8220;Simulate Download&#8221; method, you&#8217;ll get a much better idea of how your site will behave on the Web.</p>
<h4>8. Build buttons and other UI components as vector artwork in Flash</h4>
<p>One of my biggest pet peeves is using raster images for buttons that could easily be made as vector art in Flash.  Not only does this make having to update the button a multi-step process, but it also beefs up the size of the file.  It may not be much, but if you get a dozen different image buttons, you could easily have just added 100+ kb to your file weight.  Might not seem by much, but as people&#8217;s attention spans are getting shorter and shorter, that added 2 seconds of load time you just added might be costly, and you may see an increase in the number of dropped users.</p>
<p>My motto is this: &#8220;<em>If it can be made in Flash, make it in Flash&#8221;</em>.  Vector art is not only easily editable, but also a huge file weight saver.  Plus, you don&#8217;t get any degradation of quality that you&#8217;d get if you used images.  All of your lines, colors, gradients, etc. are crystal clear.  Pair that with the almost non-existent file weight they carry, and what&#8217;s not to love?</p>
<h4>9. Write custom Classes for reusable code</h4>
<p>If you write Actionscript code, you use Classes.  You might not know it, but you do.  Every time you use a property of a MovieClip instance (i.e. myClip_mc.y to get the &#8220;y&#8221; position), you&#8217;re calling the MovieClip class.  Same with Sprites, Buttons, UI Components, etc.  Classes are a staple in object-oriented programming, and allow you to read or set properties, call functions (methods), and respond to events.  It&#8217;s a great way to reuse the same code over and over again, by simply importing the classes into your site file.</p>
<p>One of my personal favorite classes that I have written is for an Email Validator, which contains the logic to check whether a string is a valid email address or not.  You can check it out in my previous post on <a href="http://www.hesslerdesign.com/blog/flash-actionscript/building-a-better-email-validator-in-flash/" target="_self">Building a Better Email Validator in Flash</a>.  Each time I need to use this, I simply import it into my file (which is 1 line of code), make an instance of the validator (1 line of code), then call the validate function and pass in the string to validate (1 line of code).  In all, for me to validate an email address, it takes 3 lines of code.  Much better than rewriting all of the validation logic in a dozen different files, eh?</p>
<h4>10. Use FlashVars to set important global variables</h4>
<p>Every so often, you&#8217;ll run into an issue where you&#8217;ll want to change a variable from outside of your Flash file. An easy way to do this is to use the FlashVars parameter when embedding your Flash file in HTML.  I find that I mostly use FlashVars for variables that will change from a &#8220;staging&#8221; site to the final &#8220;production&#8221; site.  The most often used variable that I personally encounter is the account name for Omniture tracking. The bulk of sites I develop have different staging and production Omniture account names, so the metrics don&#8217;t get skewed while testing.  But if I embed that account name into the Flash file, I suddenly need 2 different versions of the Flash file &#8212; one for staging, one for production.  But if I have a FlashVar set up for this account name, I can just change the variable in the HTML code, and have Flash read it in.  Trust me, it&#8217;s a handy way to avoid having multiple versions of the same file for different environments.</p>
<h4>In Conclusion&#8230;</h4>
<p>So there you have it.  10 easy ways to optimize your Flash sites.  There are many more ways to optimize Flash sites, and I&#8217;m sure I&#8217;ll get into them in later posts.  But these are some of the main culprits.  And trust me, individual posts could be written about each one of these (and some already have).  If you want to see posts that dive deeper into any of these, just let me know and I&#8217;ll write it up.  Until next time, happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/flash/top-ten-ways-to-optimize-your-flash-website/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Streaming RSS Feeds into Your Flash Application</title>
		<link>http://www.hesslerdesign.com/blog/flash/streaming-rss-feeds-into-your-flash-application/</link>
		<comments>http://www.hesslerdesign.com/blog/flash/streaming-rss-feeds-into-your-flash-application/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 21:30:35 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[rss]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=300</guid>
		<description><![CDATA[One of the great things about RSS feeds is that RSS formats are specified using XML.  Because of this, you can parse the RSS feed in your Flash applications just as you would an XML file.  So if, for instance, you have a Flash-based website that you would like to add a &#8220;Recent Blog Posts&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things about RSS feeds is that RSS formats are specified using XML.  Because of this, you can parse the RSS feed in your Flash applications just as you would an XML file.  So if, for instance, you have a Flash-based website that you would like to add a &#8220;Recent Blog Posts&#8221; section to, you can just import the RSS into Flash and *BAM*, you&#8217;re all set.  Here&#8217;s a quick rundown of how to do it:</p>
<h4>Step 1: Locate the RSS Feed</h4>
<p>If you&#8217;re the owner of a blog that you want to get the RSS feed to, all you should need to do is go to the blog&#8217;s home page, and add &#8220;/feed/&#8221; to the end of the URL.  (For example: <a title="Hessler Design Blog Feed" href="http://www.hesslerdesign.com/blog/feed/" target="_blank">http://www.hesslerdesign.com/blog/feed/</a>)  This is an XML-based page that gives you a list of the blog&#8217;s posts, titles, contents/extracts, etc.</p>
<p>If you need an RSS feed through a service like FeedBurner, you first need to go to the FeedBurner page of the site you want an RSS feed from. (For example: <a title="Hessler Design Blog Feed" href="http://feeds.feedburner.com/hesslerdesign" target="_blank">http://feeds.feedburner.com/hesslerdesign</a>)  This page is NOT the one you need to link to, however.  At this page you&#8217;ll notice a link to &#8220;View Feed XML&#8221; in the Subscribe Now! box.  Click on that link, and you&#8217;ll be presented with a page exactly like that of the &#8220;/feed/&#8221; URL mentioned in the previous paragraph.</p>
<p>Once you get to the XML-based page, copy the URL, because you&#8217;ll need it in Flash.  (If you want to confirm that the page is the correct one, view the page&#8217;s source. It will be set up as an XML file, with a line like <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code> at the very top.)</p>
<h4>Step 2: Importing the RSS Feed into Flash</h4>
<p>To import the feed into Flash, just follow the normal steps you would to import an XML file.  Create an <code>XML</code> object and a <code>URLLoader</code> object, then load your RSS feed URL using the <code>URLLoader</code>.  Since I&#8217;m the type of developer who likes to code as dynamically and flexibly as possible, I like to also set up an empty array for each of the items I want to hold from the RSS feed, such as titles and permalinks.  Then, when I parse the XML, I assign a slot in the appropriate array for each title and accompanying permalink, and then have them to use when I display the Recent Posts section.  Here&#8217;s how to do this:</p>
<div style="margin-left: 24px;"><code><span style="color: #808080;">/* Begin ActionScript 3.0 Code */</span><br />
<span style="color: #0000ff;">var</span> rssXML:<span style="color: #0000ff;">XML</span>;<br />
<span style="color: #0000ff;">var</span> rssLoader:<span style="color: #0000ff;">URLLoader</span>;<br />
<span style="color: #0000ff;">var</span> rssTitles_arr:<span style="color: #0000ff;">Array</span> = <span style="color: #0000ff;">new Array</span>();<br />
<span style="color: #0000ff;">var</span> rssLinks_arr:<span style="color: #0000ff;">Array</span> = <span style="color: #0000ff;">new Array</span>();<br />
<span style="color: #0000ff;">function</span> importFeed():<span style="color: #0000ff;">void</span> {<br />
rssLoader = <span style="color: #0000ff;">new URLLoader</span>();<br />
rssLoader.<span style="color: #0000ff;">addEventListener</span>(<span style="color: #0000ff;">Event.COMPLETE</span>, loadRSS);<br />
rssLoader.<span style="color: #0000ff;">load</span>(<span style="color: #0000ff;">new URLRequest</span>(<span style="color: #009900;">"http://www.hesslerdesign.com/blog/feed/"</span>));<br />
}<br />
<span style="color: #0000ff;">function</span> loadRSS(e:<span style="color: #0000ff;">Event</span>):void {<br />
rssXML = <span style="color: #0000ff;">new XML</span>(e.<span style="color: #0000ff;">target.data</span>);<br />
<span style="color: #0000ff;">trace</span>(rssXML);<br />
<span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i=0; i&lt;4; i++) {<br />
rssTitles_arr[i] = rssXML.channel.item[i].title;<br />
rssLinks_arr[i] = rssXML.channel.item[i].link;<br />
}<br />
<span style="color: #808080;">// Garbage Collection</span><br />
rssLoader.<span style="color: #0000ff;">removeEventListener</span>(<span style="color: #0000ff;">Event.COMPLETE</span>, loadRSS);<br />
}<br />
importFeed();<br />
<span style="color: #808080;">/* End ActionScript 3.0 Code */</span></code></div>
<h4>An important note:</h4>
<p>To dig down into the XML and get things like Post titles and permalinks, you need to be familiar with the structure of the RSS file.  You can see from the code above that the title for each post is inside an item tag, which sits inside of the channel tag.  If you want to get more info than just these two items, just go to the RSS URL (see Step 1) and view the page source.  Or, you can do as I&#8217;ve done in the code above and trace out the XML in Flash once the XML loads.  This is a helpful first step to see what the structure of the XML file is before you get into parsing the XML into appropriate arrays.</p>
<p>Another quick thing to note is in the <code>for</code> loop, inside of the <code>loadRSS()</code> function.  I used the number 4 as a benchmark to populate my arrays (seen in the snippet <code>i&lt;4</code>).  You can use a different number if you want.  It&#8217;s a matter of (a) the number of posts available in the RSS feed, and (b) personal preference.</p>
<p>Once you incorporate the code above, you can then use the arrays populated with RSS info and populate text fields, buttons, etc.  Since it&#8217;s stored in an array, the possibilities are virtually limitless.</p>
<p>So there you have it.  The flexibility of RSS feeds makes it just that easy to import into Flash.  Until next time, happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/flash/streaming-rss-feeds-into-your-flash-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ensuring Success by Setting Up for Failure</title>
		<link>http://www.hesslerdesign.com/blog/flash/ensuring-success-by-setting-up-for-failure/</link>
		<comments>http://www.hesslerdesign.com/blog/flash/ensuring-success-by-setting-up-for-failure/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 15:49:52 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[event listeners]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=123</guid>
		<description><![CDATA[One of the great things about Adobe Flash and ActionScript 3.0 that I really like is the range of listeners available.  Specifically, I&#8217;ve been finding myself using the IOErrorEvent a lot recently, because I do a lot of work with external files, loading XML, images, and talking to databases.  Every so often, due to lack [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things about Adobe Flash and ActionScript 3.0 that I really like is the range of listeners available.  Specifically, I&#8217;ve been finding myself using the <code>IOErrorEvent</code> a lot recently, because I do a lot of work with external files, loading XML, images, and talking to databases.  Every so often, due to lack of Internet connection or an incorrectly typed path, I&#8217;ll find an error where my Flash application breaks.  While I obviously use the <code>Event.COMPLETE</code> listener for send and load operations, I wasn&#8217;t always using the a listener for send and load failures.  That&#8217;s where the <code>IOErrorEvent</code> comes in handy.</p>
<p>The <code>IOErrorEvent</code> object kicks in when there is an error sending of loading.  There is only one type of error event that fires for this, and that is <code>IO_ERROR</code>.  If you attach the <code>IOErrorEvent.IO_ERROR</code> to your send/load operation, then you can customize an error message and/or action for your user to see if your application encounters the error.  Here&#8217;s a quick example:</p>
<p><code><span style="color: #0000ff;">var</span> myLoader:<span style="color: #0000ff;">URLLoader</span> = <span style="color: #0000ff;">new URLLoader</span>();</code><br />
<code>myLoader<span style="color: #0000ff;">.addEventListener</span>(<span style="color: #0000ff;">Event.COMPLETE</span>, loadXML);</code><br />
<code>myLoader<span style="color: #0000ff;">.addEventListener</span>(<span style="color: #0000ff;">IOErrorEvent.IO_ERROR</span>, showXMLError);</code><br />
<code>myLoader<span style="color: #0000ff;">.load</span>(<span style="color: #0000ff;">new URLRequest</span>(<span style="color: #009900;">"sample.xml"</span>));</code></p>
<p><code><span style="color: #0000ff;">function</span> loadXML(e:<span style="color: #0000ff;">Event</span>):<span style="color: #0000ff;">void</span> {</code><br />
<code><span style="color: #999999;">&nbsp; &nbsp;// Insert load actions</span></code><br />
<code>}</code></p>
<p><code><span style="color: #0000ff;">function</span> showXMLError(e:<span style="color: #0000ff;">IOErrorEvent</span>):<span style="color: #0000ff;">void</span> {</code><br />
<code><span style="color: #999999;">&nbsp; &nbsp;// Insert error actions</span></code><br />
<code>}</code></p>
<p>In this code, you&#8217;ll notice the <code>URLLoader</code> that loads a file named &#8220;sample.xml&#8221;.  There is a listener set up for when the XML file is loaded, and one for if an error is produced.  Then, the two functions are set up to contain the actions for each of the two outcomes.</p>
<p>For error functions you can do many things to alert users.  For instance, you can create and populate a text field with an error message.  Or maybe show a custom error icon.  You can even include a link or button to a contact form where they can send you (or tech support) a message about the error.  Or a button to restart the application or try to perform the failed action again.  The possibilities are endless.</p>
<p>So next time you&#8217;re programming a Flash application, consider using the IOErrorEvent to catch your errors and give your users meaningful error messages.  After all, there&#8217;s nothing worse than losing the interest or trust of your clients and users than to have an application that stops working with no hope of a fix or any sort of helpful error message.</p>
<p>Until next time, happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/flash/ensuring-success-by-setting-up-for-failure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using &amp; Formatting HTML Text in Flash</title>
		<link>http://www.hesslerdesign.com/blog/flash/using-formatting-html-text-in-flash/</link>
		<comments>http://www.hesslerdesign.com/blog/flash/using-formatting-html-text-in-flash/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 04:05:41 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[cdata]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[stylesheet]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=74</guid>
		<description><![CDATA[I import XML into my Flash projects a lot. From time to time, the XML will contain special characters that can&#8217;t be rendered by Flash.  Or maybe it will contain text that should act as a hyperlink, or be bold or italic.  Whatever the case may be, there are a few key steps to successfully [...]]]></description>
			<content:encoded><![CDATA[<p>I import XML into my Flash projects a lot. From time to time, the XML will contain special characters that can&#8217;t be rendered by Flash.  Or maybe it will contain text that should act as a hyperlink, or be bold or italic.  Whatever the case may be, there are a few key steps to successfully importing and displaying these special characters and HTML styles.</p>
<p>First, we need to set up the XML file.  In the text editor of your choice, you can begin setting it up by entering the following code:</p>
<p><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;</code><br />
<code>&lt;site&gt;</code><br />
&nbsp; &nbsp;<code>&lt;theItem&gt;&lt;/theItem&gt;</code><br />
<code>&lt;/site&gt;</code></p>
<p>In the code above, we simply set up the main tag, named <code>site</code>, and put another tag named <code>theItem</code> inside of it. (I&#8217;m not going to get into explaining best practices for setting up XML files here. That&#8217;s a topic for another post.)  Inside the <code>theItem</code> tag, we can put our text. I&#8217;ll start by putting one sentence, contained inside of a <code>&lt;p&gt;</code> tag, with <code>&lt;b&gt;</code>, <code>&lt;i&gt;</code>, and <code>&lt;a&gt;</code> HTML tags.</p>
<p><code>&lt;theItem&gt;&lt;p&gt;This is an example of XML text with &lt;b&gt;bold&lt;/b&gt; and &lt;i&gt;italic&lt;/i&gt; words, as well as &lt;a href="#"&gt;hyperlinks&lt;/a&gt;.&lt;/p&gt;&lt;/theItem&gt;</code></p>
<p>Save your XML file as &#8220;text.xml&#8221;.</p>
<p><span id="more-74"></span></p>
<p>Now that we have the XML built, let&#8217;s move over to Adobe Flash and import the XML.  With Adobe Flash open, create a new ActionScript 3.0 file, and make a new dynamic text field and name it &#8220;text_txt&#8221;.  Set the color, font, and size to your own liking.  Then, create a new layer, named Actions, and lock it.  (I always name my ActionScript layer &#8220;Actions&#8221;, lock it, and put it as the top layer.  It&#8217;s a good practice to get into if you don&#8217;t already.)  Save your Flash file in the same directory location as the XML file.</p>
<p>Now, in your Actions panel, type the following code:</p>
<p><code><span style="color: #0000ff;">var</span> theXML:<span style="color: #0000ff;">XML</span>;</code><br />
<code><span style="color: #0000ff;">var</span> xmlData:<span style="color: #0000ff;">Array</span> = <span style="color: #0000ff;">new Array</span>();</code><br />
<code><span style="color: #0000ff;">var</span> loader:<span style="color: #0000ff;">URLLoader</span> = <span style="color: #0000ff;">new URLLoader</span>();</code><br />
<code>loader.<span style="color: #0000ff;">addEventListener</span>(Event.COMPLETE, processXML);</code><br />
<code>loader.<span style="color: #0000ff;">load</span>(<span style="color: #0000ff;">new URLRequest</span>(<span style="color: #009900;">"test.xml"</span>));</code></p>
<p><code><span style="color: #0000ff;">function</span> processXML(e:<span style="color: #0000ff;">Event</span>){</code><br />
&nbsp; &nbsp;<code>theXML = <span style="color: #0000ff;">new XML</span>(e<span style="color: #0000ff;">.target.data</span>);</code><br />
&nbsp; &nbsp;<code><span style="color: #0000ff;">var</span> xmlLength:<span style="color: #0000ff;">Number</span> = theXML.theItem<span style="color: #0000ff;">.length</span>();</code><br />
&nbsp; &nbsp;<code><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i=0; i&lt;xmlLength; i++){</code><br />
&nbsp; &nbsp;&nbsp; &nbsp;<code>xmlData[i] = theXML.theItem[i];</code><br />
&nbsp; &nbsp;<code>}</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.text</span> = xmlData[0];</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.multiline</span> = <span style="color: #0000ff;">true</span>;</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.wordWrap</span> = <span style="color: #0000ff;">true</span>;</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.height</span> = text_txt<span style="color: #0000ff;">.textHeight</span> + 6;</code><br />
<code>}</code></p>
<p>Here&#8217;s a quick rundown of what this code does:<br />
- Define a new XML object named <code>theXML</code><br />
- Create a new Array named <code>xmlData</code><br />
- Create a new URLLoader named <code>loader</code>.  This loader then gets an event listener that will fire the function <code>processXML</code> when loading is complete.  To load the XML file, we load a new URLRequest to the path of our XML file (which should be in the same directory as your Flash file.)<br />
- The <code>processXML</code> function then creates our XML object, filling it with the data from the loader.<br />
- Create a number variable containing the length of the XML. This is essential for what&#8217;s coming next.<br />
- Run a <code>for</code> loop starting at 0 and going through the length of the XML object.  This loop populates our Array, <code>xmlData</code>, with each <code>theItem</code> tag from the XML.<br />
- Fill the text field with the text from the first spot in the array (our only tag in this example).<br />
- Set <code>multiline</code> and <code>wordWrap</code> to true so the text won&#8217;t be limited to one line.<br />
- Set the text field height so it includes all of the text, plus a few extra pixels of buffer at the bottom.</p>
<p>Save your Flash file and test your movie.  You&#8217;ll see something very similar to the image below, which isn&#8217;t very appealing yet.</p>
<p><img class="size-full wp-image-77" title="flash_initialImport" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/06/flash_initialImport.gif" alt="Initial Import of XML" width="552" height="152" /></p>
<p>Close the test movie and return to your Actions panel.</p>
<p>In the Actions panel, we&#8217;re going to change the line of code that sets the text in our text field to the array data.  Instead of using <code>text_txt<span style="color:#0000ff;">.text</span></code>, we&#8217;re going to use <code>text_txt<span style="color:#0000ff;>.htmlText</span></code> (see below).  By setting the text field to <code>htmlText</code>, we enable the ability to accept HTML tags and display the results appropriately.</p>
<p><code>text_txt<span style="color: #0000ff;">.htmlText</span> = xmlData[0];</code></p>
<p>Save your Flash file and re-test your movie.  You&#8217;ll notice the results are much nicer, but the lines break up each time there is an HTML tag, as in the image below.  The good news is that the bold and italic tags display the text correctly, and the hyperlink does work.  But there&#8217;s really no way to tell the user that the hyperlink is actually a hyperlink.</p>
<p><img class="size-full wp-image-78" title="flash_textNoStyle" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/06/flash_textNoStyle.gif" alt="Flash Export - Text Unstyled" width="552" height="152" /></p>
<p>Close the test movie and return to your Actions panel once again.</p>
<p>To style our HTML text, we&#8217;re going to need to create a style sheet object and attach it to our text field.  To do this, we can use the following code, placed above our <code>processXML</code> function:</p>
<p><code><span style="color: #0000ff;">var</span> textStyles:<span style="color: #0000ff;">StyleSheet</span> = <span style="color: #0000ff;">new StyleSheet</span>();</code><br />
<code>textStyles<span style="color: #0000ff;">.setStyle</span>(<span style="color: #009900;">"a:link"</span>, {color:<span style="color: #009900;">'#01628b'</span>, textDecoration:<span style="color: #009900;">'underline'</span>});</code><br />
<code>textStyles<span style="color: #0000ff;">.setStyle</span>(<span style="color: #009900;">"a:hover"</span>, {color:<span style="color: #009900;">'#d68402'</span>, textDecoration:<span style="color: #009900;">'underline'</span>});</code><br />
<code>text_txt<span style="color: #0000ff;">.styleSheet</span> = textStyles;</code></p>
<p>Here, we create a new <code>StyleSheet</code> object, and set styles for <code>&lt;a&gt;</code> tags, based on the correct syntax that Flash uses.  In this example, we assigned a color and underline to our link, and to its hover state.  (For more info on <code>StyleSheets</code>, consult the <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/text/StyleSheet.html" target="_blank">Flash documentation on StyleSheets</a>.</p>
<p>Lastly, we set the <code>styleSheet</code> attribute on our text field to our <code>StyleSheet</code> object.</p>
<p>Save your Flash file and re-test your movie again.  Our lines are still broke, but our hyperlink now looks like a hyperlink.  We&#8217;re getting closer.</p>
<p><img class="size-full wp-image-79" title="flash_textStyled" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/06/flash_textStyled.gif" alt="Flash Export - Text Styled" width="552" height="152" /></p>
<p>Close the test movie, and return to your XML file.</p>
<p>Here, we will get to the key to successfully displaying the HTML content correctly, and on one line:  The CDATA tag.</p>
<p>In order to display HTML content and special characters successfully, we need to put a CDATA tag into our <code>theItem</code> tag.  The syntax of the CDATA tag starts with <code>&lt;![CDATA[</code> and ends with <code>]]&gt;</code>.  It wraps around the outside of the <code>theItem</code> content, like this:</p>
<p><code>&lt;theItem&gt;&lt;![CDATA[&lt;p&gt;This is an example of XML text with &lt;b&gt;bold&lt;/b&gt; and &lt;i&gt;italic&lt;/i&gt; words, as well as &lt;a href="#"&gt;hyperlinks&lt;/a&gt;.&lt;/p&gt;]]&gt;&lt;/theItem&gt;</code></p>
<p>Save the XML file, and return to Flash.  Once in Flash, re-rest your movie.  You&#8217;ll notice that the text now flows correctly, and is styled according to our HTML tags and style sheet data!</p>
<p><img class="size-full wp-image-75" title="flash_cdata01" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/06/flash_cdata01.gif" alt="Flash Export - CDATA #1" width="552" height="52" /></p>
<p>Ah, sweet success!  We did it!  But the fun doesn&#8217;t stop there.  Inside of the CDATA tag, we not only have the ability to successfully show HTML tags, but also special characters.  Let&#8217;s put some special characters into our XML tag and see what happens.</p>
<p>To use special characters, we will need to know the respective HTML Entity Number, which can be found at the W3 Schools website here: <a href="http://www.w3schools.com/tags/ref_entities.asp" target="_blank">HTML Entity Number Reference</a>.</p>
<p>Now that we have a reference for finding special character entity numbers, we&#8217;ll put in another sentence containing a handful of them to see how they look.  In your XML file, add another sentence to the <code>theItem</code> tag, like the one I&#8217;ve included below:</p>
<p><code>&lt;![CDATA[&lt;p&gt;This is an example of XML text with &lt;b&gt;bold&lt;/b&gt; and &lt;i&gt;italic&lt;/i&gt; words, as well as &lt;a href="#"&gt;hyperlinks&lt;/a&gt;. It also contains special characters, like &amp;#198;, &amp;amp;, &amp;#169;, &amp;#174;, and &amp;#187;.&lt;/p&gt;]]&gt;</code></p>
<p>Save the XML file, return to Flash, and re-test your movie.  Notice that the special characters show up correctly, and in line with everything else without those annoying line breaks!</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="60" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/06/flashXML_specialCharacters.swf" /><embed type="application/x-shockwave-flash" width="550" height="60" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/06/flashXML_specialCharacters.swf"></embed></object></p>
<p>Now, I will say this:  CDATA tags aren&#8217;t needed to merely display HTML tags and special characters within Flash. Flash has done a good job with the <code>htmlText</code> attribute, but we still need to set up the XML file with CDATA tags to make the content flow properly without the line breaks.</p>
<p>Here&#8217;s a look at our final code for this test:</p>
<p><code><span style="color: #999999;">&lt;!-- Begin XML --&gt;</span></code><br />
<code>&lt;?xml version="1.0" encoding="utf-8"?&gt;</code><br />
<code>&lt;site&gt;</code><br />
&nbsp; &nbsp;<code>&lt;theItem&gt;</code><br />
&nbsp; &nbsp;&nbsp; &nbsp;<code>&lt;![CDATA[&lt;p&gt;This is an example of XML text with &lt;b&gt;bold&lt;/b&gt; and &lt;i&gt;italic&lt;/i&gt; words, as well as &lt;a href="#"&gt;hyperlinks&lt;/a&gt;. It also contains special characters, like &amp;#198;, &amp;amp;, &amp;#169;, &amp;#174;, and &amp;#187;.&lt;/p&gt;]]&gt;</code><br />
&nbsp; &nbsp;<code>&lt;/theItem&gt;</code><br />
<code>&lt;/site&gt;</code><br />
<code><span style="color: #999999;">&lt;!-- End XML --&gt;</span></code></p>
<p><code><span style="color: #999999;">/* Begin ActionScript */</span></code><br />
<code><span style="color: #0000ff;">var</span> theXML:<span style="color: #0000ff;">XML</span>;</code><br />
<code><span style="color: #0000ff;">var</span> xmlData:<span style="color: #0000ff;">Array</span> = <span style="color: #0000ff;">new Array</span>();</code><br />
<code><span style="color: #0000ff;">var</span> loader:<span style="color: #0000ff;">URLLoader</span> = <span style="color: #0000ff;">new URLLoader</span>();</code><br />
<code>loader.<span style="color: #0000ff;">addEventListener</span>(<span style="color: #0000ff;">Event.COMPLETE</span>, processXML);</code><br />
<code>loader.<span style="color: #0000ff;">load</span>(<span style="color: #0000ff;">new URLRequest</span>(<span style="color: #009900;">"test.xml"</span>));</code></p>
<p><code><span style="color: #0000ff;">var</span> textStyles:<span style="color: #0000ff;">StyleSheet</span> = new StyleSheet();</code><br />
<code>textStyles.<span style="color: #0000ff;">setStyle</span>(<span style="color: #009900;">"a:link"</span>, {color:<span style="color: #009900;">'#01628b'</span>, textDecoration:<span style="color: #009900;">'underline'</span>});</code><br />
<code>textStyles.<span style="color: #0000ff;">setStyle</span>(<span style="color: #009900;">"a:hover"</span>, {color:<span style="color: #009900;">'#d68402'</span>, textDecoration:<span style="color: #009900;">'underline'</span>});</code><br />
<code>text_txt.<span style="color: #0000ff;">styleSheet</span> = textStyles;</code></p>
<p><code><span style="color: #0000ff;">function</span> processXML(e:<span style="color: #0000ff;">Event</span>){</code><br />
&nbsp; &nbsp;<code>theXML = <span style="color: #0000ff;">new XML</span>(e<span style="color: #0000ff;">.target.data</span>);</code><br />
&nbsp; &nbsp;<code><span style="color: #0000ff;">var</span> xmlLength:<span style="color: #0000ff;">Number</span> = theXML.theItem.<span style="color: #0000ff;">length</span>();</code><br />
&nbsp; &nbsp;<code><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i=0; i&lt;xmlLength; i++){</code><br />
&nbsp; &nbsp;&nbsp; &nbsp;<code>xmlData[i] = theXML.theItem[i];</code><br />
&nbsp; &nbsp;<code>}</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.htmlText</span> = xmlData[0];</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.multiline</span> = <span style="color: #0000ff;">true</span>;</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.wordWrap</span> = <span style="color: #0000ff;">true</span>;</code><br />
&nbsp; &nbsp;<code>text_txt<span style="color: #0000ff;">.height</span> = text_txt<span style="color: #0000ff;">.textHeight</span> + 6;</code><br />
<code>}</code><br />
<code><span style="color: #999999;">/* End ActionScript */</span></code></p>
<p>If you want the source files, you can download them here.  <a href="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/09/flashXML_specialCharacters.zip">Download Source Files »</a></p>
<p>I hope that this sheds some light on the topic of getting HTML text to correctly display and flow inside of your Flash application.  Until next time, happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/flash/using-formatting-html-text-in-flash/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

